Skip to content

Commit f2787cf

Browse files
committed
Same method filtering in ConstructorResolver and getTypeForFactoryMethod
Issue: SPR-16999
1 parent dcbaece commit f2787cf

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -724,19 +724,18 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
724724
int minNrOfArgs =
725725
(mbd.hasConstructorArgumentValues() ? mbd.getConstructorArgumentValues().getArgumentCount() : 0);
726726
Method[] candidates = ReflectionUtils.getUniqueDeclaredMethods(factoryClass);
727-
for (Method factoryMethod : candidates) {
728-
if (Modifier.isStatic(factoryMethod.getModifiers()) == isStatic &&
729-
factoryMethod.getName().equals(mbd.getFactoryMethodName()) &&
730-
factoryMethod.getParameterCount() >= minNrOfArgs) {
727+
for (Method candidate : candidates) {
728+
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate) &&
729+
candidate.getParameterCount() >= minNrOfArgs) {
731730
// Declared type variables to inspect?
732-
if (factoryMethod.getTypeParameters().length > 0) {
731+
if (candidate.getTypeParameters().length > 0) {
733732
try {
734733
// Fully resolve parameter names and argument values.
735-
Class<?>[] paramTypes = factoryMethod.getParameterTypes();
734+
Class<?>[] paramTypes = candidate.getParameterTypes();
736735
String[] paramNames = null;
737736
ParameterNameDiscoverer pnd = getParameterNameDiscoverer();
738737
if (pnd != null) {
739-
paramNames = pnd.getParameterNames(factoryMethod);
738+
paramNames = pnd.getParameterNames(candidate);
740739
}
741740
ConstructorArgumentValues cav = mbd.getConstructorArgumentValues();
742741
Set<ConstructorArgumentValues.ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
@@ -753,9 +752,9 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
753752
}
754753
}
755754
Class<?> returnType = AutowireUtils.resolveReturnTypeForFactoryMethod(
756-
factoryMethod, args, getBeanClassLoader());
757-
uniqueCandidate = (commonType == null && returnType == factoryMethod.getReturnType() ?
758-
factoryMethod : null);
755+
candidate, args, getBeanClassLoader());
756+
uniqueCandidate = (commonType == null && returnType == candidate.getReturnType() ?
757+
candidate : null);
759758
commonType = ClassUtils.determineCommonAncestor(returnType, commonType);
760759
if (commonType == null) {
761760
// Ambiguous return types found: return null to indicate "not determinable".
@@ -769,8 +768,8 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
769768
}
770769
}
771770
else {
772-
uniqueCandidate = (commonType == null ? factoryMethod : null);
773-
commonType = ClassUtils.determineCommonAncestor(factoryMethod.getReturnType(), commonType);
771+
uniqueCandidate = (commonType == null ? candidate : null);
772+
commonType = ClassUtils.determineCommonAncestor(candidate.getReturnType(), commonType);
774773
if (commonType == null) {
775774
// Ambiguous return types found: return null to indicate "not determinable".
776775
return null;

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.Target;
2323
import java.util.Arrays;
2424
import java.util.List;
25+
import java.util.Map;
2526
import javax.annotation.PostConstruct;
2627

2728
import org.junit.Before;
@@ -809,6 +810,15 @@ public void testCollectionInjectionFromSameConfigurationClass() {
809810
assertSame(ctx.getBean(TestBean.class), bean.testBeans.get(0));
810811
}
811812

813+
@Test
814+
public void testMapInjectionFromSameConfigurationClass() {
815+
ApplicationContext ctx = new AnnotationConfigApplicationContext(MapInjectionConfiguration.class);
816+
MapInjectionConfiguration bean = ctx.getBean(MapInjectionConfiguration.class);
817+
assertNotNull(bean.testBeans);
818+
assertEquals(1, bean.testBeans.size());
819+
assertSame(ctx.getBean(Runnable.class), bean.testBeans.get("testBean"));
820+
}
821+
812822
@Test
813823
public void testBeanLookupFromSameConfigurationClass() {
814824
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanLookupConfiguration.class);
@@ -1566,6 +1576,23 @@ public TestBean thing() {
15661576
}
15671577
}
15681578

1579+
@Configuration
1580+
public static class MapInjectionConfiguration {
1581+
1582+
@Autowired
1583+
private Map<String, Runnable> testBeans;
1584+
1585+
@Bean
1586+
Runnable testBean() {
1587+
return () -> {};
1588+
}
1589+
1590+
// Unrelated, not to be considered as a factory method
1591+
private boolean testBean(boolean param) {
1592+
return param;
1593+
}
1594+
}
1595+
15691596
@Configuration
15701597
static abstract class BeanLookupConfiguration {
15711598

0 commit comments

Comments
 (0)