Skip to content

Commit

Permalink
Consistent throwing of last UnsatisfiedDependencyException if availab…
Browse files Browse the repository at this point in the history
…le and no constructor resolved

Issue: SPR-12543
  • Loading branch information
jhoeller committed Dec 23, 2014
1 parent cae217d commit d55af2b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefi
AutowireUtils.sortConstructors(candidates);
int minTypeDiffWeight = Integer.MAX_VALUE;
Set<Constructor<?>> ambiguousConstructors = null;
List<Exception> causes = null;
LinkedList<UnsatisfiedDependencyException> causes = null;

for (int i = 0; i < candidates.length; i++) {
Constructor<?> candidate = candidates[i];
Expand Down Expand Up @@ -190,22 +190,12 @@ public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefi
this.beanFactory.logger.trace(
"Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
}
if (i == candidates.length - 1 && constructorToUse == null) {
if (causes != null) {
for (Exception cause : causes) {
this.beanFactory.onSuppressedException(cause);
}
}
throw ex;
}
else {
// Swallow and try next constructor.
if (causes == null) {
causes = new LinkedList<Exception>();
}
causes.add(ex);
continue;
// Swallow and try next constructor.
if (causes == null) {
causes = new LinkedList<UnsatisfiedDependencyException>();
}
causes.add(ex);
continue;
}
}
else {
Expand Down Expand Up @@ -236,6 +226,13 @@ else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
}

if (constructorToUse == null) {
if (causes != null) {
UnsatisfiedDependencyException ex = causes.removeLast();
for (Exception cause : causes) {
this.beanFactory.onSuppressedException(cause);
}
throw ex;
}
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Could not resolve matching constructor " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,30 @@ public void testAvoidCircularReferenceThroughAutowiring() {
lbf.preInstantiateSingletons();
}

@Test
public void testConstructorDependencyWithClassResolution() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.String");
lbf.registerBeanDefinition("test", bd);
lbf.preInstantiateSingletons();
}

@Test
public void testConstructorDependencyWithUnresolvableClass() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin");
lbf.registerBeanDefinition("test", bd);
try {
lbf.preInstantiateSingletons();
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException expected) {
assertTrue(expected.toString().contains("java.lang.Strin"));
}
}

@Test
public void testBeanDefinitionWithInterface() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Expand All @@ -2033,7 +2057,7 @@ public void testBeanDefinitionWithInterface() {
}
catch (BeanCreationException ex) {
assertEquals("test", ex.getBeanName());
assertTrue(ex.getMessage().toLowerCase().indexOf("interface") != -1);
assertTrue(ex.getMessage().toLowerCase().contains("interface"));
}
}

Expand All @@ -2047,7 +2071,7 @@ public void testBeanDefinitionWithAbstractClass() {
}
catch (BeanCreationException ex) {
assertEquals("test", ex.getBeanName());
assertTrue(ex.getMessage().toLowerCase().indexOf("abstract") != -1);
assertTrue(ex.getMessage().toLowerCase().contains("abstract"));
}
}

Expand Down Expand Up @@ -2739,6 +2763,16 @@ public boolean isSingleton() {
}


public static class ConstructorDependencyWithClassResolution {

public ConstructorDependencyWithClassResolution(Class<?> clazz) {
}

public ConstructorDependencyWithClassResolution() {
}
}


public static class BeanWithDisposableBean implements DisposableBean {

private static boolean closed;
Expand Down

0 comments on commit d55af2b

Please sign in to comment.