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
This commit is contained in:
Phillip Webb
2013-01-22 17:53:30 -08:00
parent 5fb75304eb
commit 7e74fd2b7f
2 changed files with 48 additions and 0 deletions

View File

@@ -90,6 +90,7 @@ import org.springframework.util.StringUtils;
* @author Sam Brannen
* @author Costin Leau
* @author Chris Beams
* @author Phillip Webb
* @since 16 April 2001
* @see StaticListableBeanFactory
* @see PropertiesBeanDefinitionReader
@@ -272,6 +273,20 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return getBean(beanNames[0], requiredType);
}
else if (beanNames.length > 1) {
T primaryBean = null;
for (String beanName : beanNames) {
T beanInstance = getBean(beanName, requiredType);
if (isPrimary(beanName, beanInstance)) {
if(primaryBean != null) {
throw new NoUniqueBeanDefinitionException(requiredType, beanNames.length,
"more than one 'primary' bean found of required type: " + Arrays.asList(beanNames));
}
primaryBean = beanInstance;
}
}
if(primaryBean != null) {
return primaryBean;
}
throw new NoUniqueBeanDefinitionException(requiredType, beanNames);
}
else if (getParentBeanFactory() != null) {