Skip to content

Commit 1b53edf

Browse files
committed
DefaultListableBeanFactory skips fallback match attempt for Collection/Map beans
Issue: SPR-13963
1 parent 0adc492 commit 1b53edf

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ protected Map<String, Object> findAutowireCandidates(
11991199
result.put(candidateName, getBean(candidateName));
12001200
}
12011201
}
1202-
if (result.isEmpty()) {
1202+
if (result.isEmpty() && !Collection.class.isAssignableFrom(requiredType) && !Map.class.isAssignableFrom(requiredType)) {
12031203
// Consider fallback matches if the first pass failed to find anything...
12041204
DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
12051205
for (String candidateName : candidateNames) {

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

+31-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.lang.reflect.Method;
2626
import java.lang.reflect.Proxy;
2727
import java.util.Collections;
28+
import java.util.HashMap;
2829
import java.util.HashSet;
2930
import java.util.LinkedHashMap;
3031
import java.util.LinkedHashSet;
@@ -910,16 +911,15 @@ public void testConstructorInjectionWithPlainMapAsBean() {
910911
RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class);
911912
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
912913
bf.registerBeanDefinition("annotatedBean", bd);
913-
Map<String, TestBean> tbm = new LinkedHashMap<String, TestBean>();
914-
tbm.put("testBean1", new TestBean("tb1"));
915-
tbm.put("testBean2", new TestBean("tb2"));
916-
bf.registerSingleton("testBeans", tbm);
917-
bf.registerSingleton("otherMap", new Properties());
914+
RootBeanDefinition tbm = new RootBeanDefinition(CollectionFactoryMethods.class);
915+
tbm.setUniqueFactoryMethodName("testBeanMap");
916+
bf.registerBeanDefinition("myTestBeanMap", tbm);
917+
bf.registerSingleton("otherMap", new HashMap<Object, Object>());
918918

919919
MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
920-
assertSame(tbm, bean.getTestBeanMap());
920+
assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap());
921921
bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
922-
assertSame(tbm, bean.getTestBeanMap());
922+
assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap());
923923
}
924924

925925
@Test
@@ -954,16 +954,15 @@ public void testConstructorInjectionWithPlainSetAsBean() {
954954
RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class);
955955
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
956956
bf.registerBeanDefinition("annotatedBean", bd);
957-
Set<TestBean> tbs = new LinkedHashSet<TestBean>();
958-
tbs.add(new TestBean("tb1"));
959-
tbs.add(new TestBean("tb2"));
960-
bf.registerSingleton("testBeanSet", tbs);
957+
RootBeanDefinition tbs = new RootBeanDefinition(CollectionFactoryMethods.class);
958+
tbs.setUniqueFactoryMethodName("testBeanSet");
959+
bf.registerBeanDefinition("myTestBeanSet", tbs);
961960
bf.registerSingleton("otherSet", new HashSet<Object>());
962961

963962
SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean");
964-
assertSame(tbs, bean.getTestBeanSet());
963+
assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet());
965964
bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean");
966-
assertSame(tbs, bean.getTestBeanSet());
965+
assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet());
967966
}
968967

969968
@Test
@@ -3203,4 +3202,23 @@ public ProvidedArgumentBean(String[] args) {
32033202
}
32043203
}
32053204

3205+
3206+
public static class CollectionFactoryMethods {
3207+
3208+
public static Map<String, TestBean> testBeanMap() {
3209+
Map<String, TestBean> tbm = new LinkedHashMap<String, TestBean>();
3210+
tbm.put("testBean1", new TestBean("tb1"));
3211+
tbm.put("testBean2", new TestBean("tb2"));
3212+
return tbm;
3213+
}
3214+
3215+
public static Set<TestBean> testBeanSet() {
3216+
Set<TestBean> tbs = new LinkedHashSet<TestBean>();
3217+
tbs.add(new TestBean("tb1"));
3218+
tbs.add(new TestBean("tb2"));
3219+
return tbs;
3220+
}
3221+
3222+
}
3223+
32063224
}

0 commit comments

Comments
 (0)