Skip to content

Commit e6d3c28

Browse files
committed
Avoid instantiation of non-selected beans in BeanFactoryAnnotationUtils.qualifiedBeanOfType
Issue: SPR-13741 (cherry picked from commit 8ed2c47)
1 parent a730b6e commit e6d3c28

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java

Lines changed: 6 additions & 8 deletions
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.
@@ -17,7 +17,6 @@
1717
package org.springframework.beans.factory.annotation;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.Map;
2120

2221
import org.springframework.beans.factory.BeanFactory;
2322
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -77,20 +76,19 @@ else if (beanFactory.containsBean(qualifier)) {
7776
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
7877
*/
7978
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
80-
Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
81-
T matchingBean = null;
82-
for (Map.Entry<String, T> entry : candidateBeans.entrySet()) {
83-
String beanName = entry.getKey();
79+
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType);
80+
String matchingBean = null;
81+
for (String beanName : candidateBeans) {
8482
if (isQualifierMatch(qualifier, beanName, bf)) {
8583
if (matchingBean != null) {
8684
throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() +
8785
" bean found for qualifier '" + qualifier + "'");
8886
}
89-
matchingBean = entry.getValue();
87+
matchingBean = beanName;
9088
}
9189
}
9290
if (matchingBean != null) {
93-
return matchingBean;
91+
return bf.getBean(matchingBean, beanType);
9492
}
9593
else if (bf.containsBean(qualifier)) {
9694
// Fallback: target bean at least found by bean name - probably a manually registered singleton.

0 commit comments

Comments
 (0)