Skip to content

Commit 7e74fd2

Browse files
committed
Consider primary attribute with getBean(Class)
Update DefaultListableBeanFactory.getBean(Class<?> beanClass) to consider the 'primary' attribute of bean definitions. This makes getBean() behave in the same way as autowiring. Issue: SPR-7854
1 parent 5fb7530 commit 7e74fd2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
* @author Sam Brannen
9191
* @author Costin Leau
9292
* @author Chris Beams
93+
* @author Phillip Webb
9394
* @since 16 April 2001
9495
* @see StaticListableBeanFactory
9596
* @see PropertiesBeanDefinitionReader
@@ -272,6 +273,20 @@ public <T> T getBean(Class<T> requiredType) throws BeansException {
272273
return getBean(beanNames[0], requiredType);
273274
}
274275
else if (beanNames.length > 1) {
276+
T primaryBean = null;
277+
for (String beanName : beanNames) {
278+
T beanInstance = getBean(beanName, requiredType);
279+
if (isPrimary(beanName, beanInstance)) {
280+
if(primaryBean != null) {
281+
throw new NoUniqueBeanDefinitionException(requiredType, beanNames.length,
282+
"more than one 'primary' bean found of required type: " + Arrays.asList(beanNames));
283+
}
284+
primaryBean = beanInstance;
285+
}
286+
}
287+
if(primaryBean != null) {
288+
return primaryBean;
289+
}
275290
throw new NoUniqueBeanDefinitionException(requiredType, beanNames);
276291
}
277292
else if (getParentBeanFactory() != null) {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.beans.factory;
1818

1919
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.Matchers.containsString;
21+
import static org.hamcrest.Matchers.equalTo;
2022
import static org.junit.Assert.assertEquals;
2123
import static org.junit.Assert.assertFalse;
2224
import static org.junit.Assert.assertNotNull;
@@ -49,7 +51,9 @@
4951
import org.apache.commons.logging.Log;
5052
import org.apache.commons.logging.LogFactory;
5153
import org.junit.Ignore;
54+
import org.junit.Rule;
5255
import org.junit.Test;
56+
import org.junit.rules.ExpectedException;
5357
import org.springframework.beans.BeansException;
5458
import org.springframework.beans.MutablePropertyValues;
5559
import org.springframework.beans.NotWritablePropertyException;
@@ -102,11 +106,14 @@
102106
* @author Rick Evans
103107
* @author Sam Brannen
104108
* @author Chris Beams
109+
* @author Phillip Webb
105110
*/
106111
public class DefaultListableBeanFactoryTests {
107112

108113
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
109114

115+
@Rule
116+
public ExpectedException thrown = ExpectedException.none();
110117

111118
@Test
112119
public void testUnreferencedSingletonWasInstantiated() {
@@ -1285,6 +1292,32 @@ public void testGetBeanByTypeWithAmbiguity() {
12851292
lbf.getBean(TestBean.class);
12861293
}
12871294

1295+
@Test
1296+
public void testGetBeanByTypeWithPrimary() throws Exception {
1297+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
1298+
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
1299+
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
1300+
bd2.setPrimary(true);
1301+
lbf.registerBeanDefinition("bd1", bd1);
1302+
lbf.registerBeanDefinition("bd2", bd2);
1303+
TestBean bean = lbf.getBean(TestBean.class);
1304+
assertThat(bean.getBeanName(), equalTo("bd2"));
1305+
}
1306+
1307+
@Test
1308+
public void testGetBeanByTypeWithMultiplePrimary() throws Exception {
1309+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
1310+
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
1311+
bd1.setPrimary(true);
1312+
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
1313+
bd2.setPrimary(true);
1314+
lbf.registerBeanDefinition("bd1", bd1);
1315+
lbf.registerBeanDefinition("bd2", bd2);
1316+
thrown.expect(NoUniqueBeanDefinitionException.class);
1317+
thrown.expectMessage(containsString("more than one 'primary'"));
1318+
lbf.getBean(TestBean.class);
1319+
}
1320+
12881321
@Test
12891322
public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
12901323
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)