Skip to content

Commit f01a030

Browse files
committed
getTypeForFactoryBean suppresses instantiation failures for non-singleton FactoryBeans
Issue: SPR-12786 (cherry picked from commit 9b25d6a)
1 parent f269190 commit f01a030

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

+8
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,14 @@ private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, R
897897
instance = bw.getWrappedInstance();
898898
}
899899
}
900+
catch (BeanCreationException ex) {
901+
// Can only happen when getting a FactoryBean.
902+
if (logger.isDebugEnabled()) {
903+
logger.debug("Bean creation exception on non-singleton FactoryBean type check: " + ex);
904+
}
905+
onSuppressedException(ex);
906+
return null;
907+
}
900908
finally {
901909
// Finished partial creation of this bean.
902910
afterPrototypeCreation(beanName);

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

+35-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1340,10 +1340,10 @@ public void testAutowireBeanByNameWithNoDependencyCheck() {
13401340
public void testDependsOnCycle() {
13411341
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
13421342
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
1343-
bd1.setDependsOn(new String[] {"tb2"});
1343+
bd1.setDependsOn("tb2");
13441344
lbf.registerBeanDefinition("tb1", bd1);
13451345
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
1346-
bd2.setDependsOn(new String[] {"tb1"});
1346+
bd2.setDependsOn("tb1");
13471347
lbf.registerBeanDefinition("tb2", bd2);
13481348
try {
13491349
lbf.preInstantiateSingletons();
@@ -1361,13 +1361,13 @@ public void testDependsOnCycle() {
13611361
public void testImplicitDependsOnCycle() {
13621362
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
13631363
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
1364-
bd1.setDependsOn(new String[] {"tb2"});
1364+
bd1.setDependsOn("tb2");
13651365
lbf.registerBeanDefinition("tb1", bd1);
13661366
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
1367-
bd2.setDependsOn(new String[] {"tb3"});
1367+
bd2.setDependsOn("tb3");
13681368
lbf.registerBeanDefinition("tb2", bd2);
13691369
RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
1370-
bd3.setDependsOn(new String[] {"tb1"});
1370+
bd3.setDependsOn("tb1");
13711371
lbf.registerBeanDefinition("tb3", bd3);
13721372
try {
13731373
lbf.preInstantiateSingletons();
@@ -1540,7 +1540,7 @@ public void testGetBeanByTypeInstanceWithAmbiguity() {
15401540
}
15411541

15421542
@Test
1543-
public void testGetBeanByTypeInstanceWithPrimary() throws Exception {
1543+
public void testGetBeanByTypeInstanceWithPrimary() {
15441544
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
15451545
RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
15461546
RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
@@ -1553,7 +1553,7 @@ public void testGetBeanByTypeInstanceWithPrimary() throws Exception {
15531553
}
15541554

15551555
@Test
1556-
public void testGetBeanByTypeInstanceWithMultiplePrimary() throws Exception {
1556+
public void testGetBeanByTypeInstanceWithMultiplePrimary() {
15571557
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
15581558
RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
15591559
RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
@@ -1584,16 +1584,35 @@ public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() {
15841584
try {
15851585
lbf.getBean(TestBean.class, 67);
15861586
fail("Should have thrown NoSuchBeanDefinitionException");
1587-
} catch (NoSuchBeanDefinitionException ex) {
1587+
}
1588+
catch (NoSuchBeanDefinitionException ex) {
15881589
// expected
15891590
}
15901591
}
15911592

1592-
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) {
1593+
@Test
1594+
public void testGetBeanWithArgsNotCreatedForFactoryBeanChecking() {
1595+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
15931596
RootBeanDefinition bd1 = new RootBeanDefinition(ConstructorDependency.class);
15941597
bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
1595-
bd1.getConstructorArgumentValues().addGenericArgumentValue(String.valueOf(age));
1596-
return bd1;
1598+
lbf.registerBeanDefinition("bd1", bd1);
1599+
RootBeanDefinition bd2 = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
1600+
bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
1601+
lbf.registerBeanDefinition("bd2", bd2);
1602+
1603+
ConstructorDependency bean = lbf.getBean(ConstructorDependency.class, 42);
1604+
assertThat(bean.beanName, equalTo("bd1"));
1605+
assertThat(bean.spouseAge, equalTo(42));
1606+
1607+
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length);
1608+
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length);
1609+
}
1610+
1611+
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) {
1612+
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependency.class);
1613+
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
1614+
bd.getConstructorArgumentValues().addGenericArgumentValue(String.valueOf(age));
1615+
return bd;
15971616
}
15981617

15991618
@Test
@@ -1664,8 +1683,8 @@ public void testAutowireBeanByTypeWithTwoMatches() {
16641683
}
16651684
catch (UnsatisfiedDependencyException ex) {
16661685
// expected
1667-
assertTrue(ex.getMessage().indexOf("test") != -1);
1668-
assertTrue(ex.getMessage().indexOf("spouse") != -1);
1686+
assertTrue(ex.getMessage().contains("test"));
1687+
assertTrue(ex.getMessage().contains("spouse"));
16691688
}
16701689
}
16711690

@@ -1682,8 +1701,8 @@ public void testAutowireBeanByTypeWithTwoMatchesAndParameterNameDiscovery() {
16821701
}
16831702
catch (UnsatisfiedDependencyException ex) {
16841703
// expected
1685-
assertTrue(ex.getMessage().indexOf("test") != -1);
1686-
assertTrue(ex.getMessage().indexOf("spouse") != -1);
1704+
assertTrue(ex.getMessage().contains("test"));
1705+
assertTrue(ex.getMessage().contains("spouse"));
16871706
}
16881707
}
16891708

0 commit comments

Comments
 (0)