getBean(Class<?>) now filters out bean definitions for which isAutowireCandidate() is false (SPR-7120)

This commit is contained in:
Chris Beams
2010-04-22 16:34:36 +00:00
parent 5057c4a69c
commit 3f06a92b6b
2 changed files with 43 additions and 0 deletions

View File

@@ -248,6 +248,17 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
public <T> T getBean(Class<T> requiredType) throws BeansException {
Assert.notNull(requiredType, "Required type must not be null");
String[] beanNames = getBeanNamesForType(requiredType);
if (beanNames.length > 1) {
ArrayList<String> autowireCandidates = new ArrayList<String>();
for (String beanName : beanNames) {
if (getBeanDefinition(beanName).isAutowireCandidate()) {
autowireCandidates.add(beanName);
}
}
if (autowireCandidates.size() > 0) {
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
}
}
if (beanNames.length == 1) {
return getBean(beanNames[0], requiredType);
}