From 8a83af55b8adf833683b8d9a6924eecf69709a12 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 26 Feb 2016 12:31:03 +0100 Subject: [PATCH] Consistent resolution of factory method exceptions Issue: SPR-13985 --- .../factory/support/ConstructorResolver.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index a3ae158217e..73ecb36ed65 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -444,7 +444,7 @@ class ConstructorResolver { minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues); } - List causes = null; + LinkedList causes = null; for (int i = 0; i < candidates.length; i++) { Method candidate = candidates[i]; @@ -469,22 +469,12 @@ class ConstructorResolver { this.beanFactory.logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex); } - if (i == candidates.length - 1 && argsHolderToUse == null) { - if (causes != null) { - for (Exception cause : causes) { - this.beanFactory.onSuppressedException(cause); - } - } - throw ex; - } - else { - // Swallow and try next overloaded factory method. - if (causes == null) { - causes = new LinkedList(); - } - causes.add(ex); - continue; + // Swallow and try next overloaded factory method. + if (causes == null) { + causes = new LinkedList(); } + causes.add(ex); + continue; } } @@ -525,6 +515,13 @@ class ConstructorResolver { } if (factoryMethodToUse == null) { + if (causes != null) { + UnsatisfiedDependencyException ex = causes.removeLast(); + for (Exception cause : causes) { + this.beanFactory.onSuppressedException(cause); + } + throw ex; + } List argTypes = new ArrayList(minNrOfArgs); if (explicitArgs != null) { for (Object arg : explicitArgs) {