Skip to content

Commit

Permalink
DefaultListableBeanFactory skips fallback match attempt for Collectio…
Browse files Browse the repository at this point in the history
…n/Map beans

Issue: SPR-13963
  • Loading branch information
jhoeller committed Feb 18, 2016
1 parent 0adc492 commit 1b53edf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ protected Map<String, Object> findAutowireCandidates(
result.put(candidateName, getBean(candidateName));
}
}
if (result.isEmpty()) {
if (result.isEmpty() && !Collection.class.isAssignableFrom(requiredType) && !Map.class.isAssignableFrom(requiredType)) {
// Consider fallback matches if the first pass failed to find anything...
DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
for (String candidateName : candidateNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -910,16 +911,15 @@ public void testConstructorInjectionWithPlainMapAsBean() {
RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class);
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", bd);
Map<String, TestBean> tbm = new LinkedHashMap<String, TestBean>();
tbm.put("testBean1", new TestBean("tb1"));
tbm.put("testBean2", new TestBean("tb2"));
bf.registerSingleton("testBeans", tbm);
bf.registerSingleton("otherMap", new Properties());
RootBeanDefinition tbm = new RootBeanDefinition(CollectionFactoryMethods.class);
tbm.setUniqueFactoryMethodName("testBeanMap");
bf.registerBeanDefinition("myTestBeanMap", tbm);
bf.registerSingleton("otherMap", new HashMap<Object, Object>());

MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
assertSame(tbm, bean.getTestBeanMap());
assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap());
bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
assertSame(tbm, bean.getTestBeanMap());
assertSame(bf.getBean("myTestBeanMap"), bean.getTestBeanMap());
}

@Test
Expand Down Expand Up @@ -954,16 +954,15 @@ public void testConstructorInjectionWithPlainSetAsBean() {
RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class);
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", bd);
Set<TestBean> tbs = new LinkedHashSet<TestBean>();
tbs.add(new TestBean("tb1"));
tbs.add(new TestBean("tb2"));
bf.registerSingleton("testBeanSet", tbs);
RootBeanDefinition tbs = new RootBeanDefinition(CollectionFactoryMethods.class);
tbs.setUniqueFactoryMethodName("testBeanSet");
bf.registerBeanDefinition("myTestBeanSet", tbs);
bf.registerSingleton("otherSet", new HashSet<Object>());

SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean");
assertSame(tbs, bean.getTestBeanSet());
assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet());
bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean");
assertSame(tbs, bean.getTestBeanSet());
assertSame(bf.getBean("myTestBeanSet"), bean.getTestBeanSet());
}

@Test
Expand Down Expand Up @@ -3203,4 +3202,23 @@ public ProvidedArgumentBean(String[] args) {
}
}


public static class CollectionFactoryMethods {

public static Map<String, TestBean> testBeanMap() {
Map<String, TestBean> tbm = new LinkedHashMap<String, TestBean>();
tbm.put("testBean1", new TestBean("tb1"));
tbm.put("testBean2", new TestBean("tb2"));
return tbm;
}

public static Set<TestBean> testBeanSet() {
Set<TestBean> tbs = new LinkedHashSet<TestBean>();
tbs.add(new TestBean("tb1"));
tbs.add(new TestBean("tb2"));
return tbs;
}

}

}

0 comments on commit 1b53edf

Please sign in to comment.