Skip to content

Commit 7bc04dc

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

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

Lines changed: 6 additions & 7 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,19 +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 (String beanName : candidateBeans.keySet()) {
79+
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType);
80+
String matchingBean = null;
81+
for (String beanName : candidateBeans) {
8382
if (isQualifierMatch(qualifier, beanName, bf)) {
8483
if (matchingBean != null) {
8584
throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() +
8685
" bean found for qualifier '" + qualifier + "'");
8786
}
88-
matchingBean = candidateBeans.get(beanName);
87+
matchingBean = beanName;
8988
}
9089
}
9190
if (matchingBean != null) {
92-
return matchingBean;
91+
return bf.getBean(matchingBean, beanType);
9392
}
9493
else if (bf.containsBean(qualifier)) {
9594
// Fallback: target bean at least found by bean name - probably a manually registered singleton.

0 commit comments

Comments
 (0)