ResolvableType-based type matching at the BeanFactory API level
Issue: SPR-12147
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,8 +23,6 @@ import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
@@ -53,16 +51,16 @@ public final class BeanFactoryUtilsTests {
|
||||
private static final Resource LEAF_CONTEXT = qualifiedResource(CLASS, "leaf.xml");
|
||||
private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource(CLASS, "dependentBeans.xml");
|
||||
|
||||
private ConfigurableListableBeanFactory listableBeanFactory;
|
||||
private DefaultListableBeanFactory listableBeanFactory;
|
||||
|
||||
private DefaultListableBeanFactory dependentBeansFactory;
|
||||
|
||||
private ConfigurableListableBeanFactory dependentBeansBF;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Interesting hierarchical factory to test counts.
|
||||
// Slow to read so we cache it.
|
||||
|
||||
|
||||
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
|
||||
DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
|
||||
@@ -70,12 +68,13 @@ public final class BeanFactoryUtilsTests {
|
||||
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
|
||||
new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);
|
||||
|
||||
this.dependentBeansBF = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.dependentBeansBF).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
|
||||
dependentBeansBF.preInstantiateSingletons();
|
||||
this.dependentBeansFactory = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(this.dependentBeansFactory).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
|
||||
dependentBeansFactory.preInstantiateSingletons();
|
||||
this.listableBeanFactory = child;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
|
||||
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
|
||||
@@ -92,22 +91,21 @@ public final class BeanFactoryUtilsTests {
|
||||
// Leaf count
|
||||
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
|
||||
// Count minus duplicate
|
||||
assertTrue("Should count 7 beans, not "
|
||||
+ BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
|
||||
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
|
||||
assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
|
||||
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHierarchicalNamesWithNoMatch() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
NoOp.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class));
|
||||
assertEquals(0, names.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
IndexedTestBean.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class));
|
||||
assertEquals(1, names.size());
|
||||
assertTrue(names.contains("indexedBean"));
|
||||
// Distinguish from default ListableBeanFactory behavior
|
||||
@@ -116,8 +114,8 @@ public final class BeanFactoryUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testGetBeanNamesForTypeWithOverride() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
ITestBean.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class));
|
||||
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
|
||||
assertEquals(4, names.size());
|
||||
assertTrue(names.contains("test"));
|
||||
@@ -147,18 +145,7 @@ public final class BeanFactoryUtilsTests {
|
||||
lbf.addBean("t3", t3);
|
||||
lbf.addBean("t4", t4);
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
|
||||
assertEquals(2, beans.size());
|
||||
assertEquals(t1, beans.get("t1"));
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
|
||||
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, false, true);
|
||||
assertEquals(3, beans.size());
|
||||
assertEquals(t1, beans.get("t1"));
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
assertEquals(t3.getObject(), beans.get("t3"));
|
||||
|
||||
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
|
||||
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
|
||||
assertEquals(4, beans.size());
|
||||
assertEquals(t1, beans.get("t1"));
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
@@ -191,8 +178,8 @@ public final class BeanFactoryUtilsTests {
|
||||
this.listableBeanFactory.registerSingleton("t3", t3);
|
||||
this.listableBeanFactory.registerSingleton("t4", t4);
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
|
||||
false);
|
||||
Map<String, ?> beans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
|
||||
assertEquals(6, beans.size());
|
||||
assertEquals(test3, beans.get("test3"));
|
||||
assertEquals(test, beans.get("test"));
|
||||
@@ -200,12 +187,9 @@ public final class BeanFactoryUtilsTests {
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
assertEquals(t3.getObject(), beans.get("t3"));
|
||||
assertTrue(beans.get("t4") instanceof TestBean);
|
||||
// t3 and t4 are found here as of Spring 2.0, since they are
|
||||
// pre-registered
|
||||
// singleton instances, while testFactory1 and testFactory are *not*
|
||||
// found
|
||||
// because they are FactoryBean definitions that haven't been
|
||||
// initialized yet.
|
||||
// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
|
||||
// singleton instances, while testFactory1 and testFactory are *not* found
|
||||
// because they are FactoryBean definitions that haven't been initialized yet.
|
||||
|
||||
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
|
||||
Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
|
||||
@@ -247,8 +231,8 @@ public final class BeanFactoryUtilsTests {
|
||||
Object test3 = this.listableBeanFactory.getBean("test3");
|
||||
Object test = this.listableBeanFactory.getBean("test");
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
|
||||
false);
|
||||
Map<?, ?> beans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
|
||||
assertEquals(2, beans.size());
|
||||
assertEquals(test3, beans.get("test3"));
|
||||
assertEquals(test, beans.get("test"));
|
||||
@@ -283,25 +267,25 @@ public final class BeanFactoryUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testADependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("a");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("a");
|
||||
assertTrue(ObjectUtils.isEmpty(deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("b");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("b");
|
||||
assertTrue(Arrays.equals(new String[] { "c" }, deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("c");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("c");
|
||||
assertTrue(Arrays.equals(new String[] { "int", "long" }, deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("int");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("int");
|
||||
assertTrue(Arrays.equals(new String[] { "buffer" }, deps));
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
|
||||
import org.springframework.beans.propertyeditors.CustomNumberEditor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
@@ -194,13 +195,16 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertTrue(lbf.isTypeMatch("x1", TestBean.class));
|
||||
assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
|
||||
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
|
||||
assertEquals(TestBean.class, lbf.getType("x1"));
|
||||
assertEquals(DummyFactory.class, lbf.getType("&x1"));
|
||||
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
|
||||
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
Properties p = new Properties();
|
||||
p.setProperty("x1.(class)", DummyFactory.class.getName());
|
||||
@@ -226,6 +230,9 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertTrue(lbf.isTypeMatch("x1", TestBean.class));
|
||||
assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
|
||||
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
|
||||
assertEquals(TestBean.class, lbf.getType("x1"));
|
||||
assertEquals(DummyFactory.class, lbf.getType("&x1"));
|
||||
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
|
||||
@@ -257,6 +264,9 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertTrue(lbf.isTypeMatch("x1", TestBean.class));
|
||||
assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
|
||||
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
|
||||
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
|
||||
assertEquals(TestBean.class, lbf.getType("x1"));
|
||||
assertEquals(DummyFactory.class, lbf.getType("&x1"));
|
||||
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
|
||||
@@ -1381,7 +1391,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=NoSuchBeanDefinitionException.class)
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void testGetBeanByTypeWithNoneFound() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.getBean(TestBean.class);
|
||||
@@ -1397,7 +1407,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertThat(bean.getBeanName(), equalTo("bd1"));
|
||||
}
|
||||
|
||||
@Test(expected=NoUniqueBeanDefinitionException.class)
|
||||
@Test(expected = NoUniqueBeanDefinitionException.class)
|
||||
public void testGetBeanByTypeWithAmbiguity() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
|
||||
@@ -1606,6 +1616,8 @@ public class DefaultListableBeanFactoryTests {
|
||||
|
||||
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length);
|
||||
assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length);
|
||||
assertEquals(1, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length);
|
||||
assertEquals(0, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length);
|
||||
}
|
||||
|
||||
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) {
|
||||
@@ -1660,7 +1672,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
* Java method names. In other words, you can't name a method
|
||||
* {@code set&FactoryBean(...)}.
|
||||
*/
|
||||
@Test(expected=TypeMismatchException.class)
|
||||
@Test(expected = TypeMismatchException.class)
|
||||
public void testAutowireBeanWithFactoryBeanByName() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class);
|
||||
@@ -2537,7 +2549,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertEquals(expectedNameFromArgs, tb2.getName());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
|
||||
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -40,6 +40,7 @@ import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.beans.propertyeditors.CustomNumberEditor;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.tests.Assume;
|
||||
@@ -770,7 +771,7 @@ public class BeanFactoryGenericsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpr11250() {
|
||||
public void testGenericMatchingWithBeanNameDifferentiation() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver());
|
||||
|
||||
@@ -780,8 +781,43 @@ public class BeanFactoryGenericsTests {
|
||||
new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false));
|
||||
|
||||
NumberBean nb = bf.getBean(NumberBean.class);
|
||||
assertNotNull(nb.getDoubleStore());
|
||||
assertNotNull(nb.getFloatStore());
|
||||
assertSame(bf.getBean("doubleStore"), nb.getDoubleStore());
|
||||
assertSame(bf.getBean("floatStore"), nb.getFloatStore());
|
||||
|
||||
String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class));
|
||||
String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class));
|
||||
String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class));
|
||||
assertEquals(2, numberStoreNames.length);
|
||||
assertEquals("doubleStore", numberStoreNames[0]);
|
||||
assertEquals("floatStore", numberStoreNames[1]);
|
||||
assertEquals(0, doubleStoreNames.length);
|
||||
assertEquals(0, floatStoreNames.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMatchingWithFullTypeDifferentiation() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver());
|
||||
|
||||
bf.registerBeanDefinition("store1", new RootBeanDefinition(DoubleStore.class));
|
||||
bf.registerBeanDefinition("store2", new RootBeanDefinition(FloatStore.class));
|
||||
bf.registerBeanDefinition("numberBean",
|
||||
new RootBeanDefinition(NumberBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false));
|
||||
|
||||
NumberBean nb = bf.getBean(NumberBean.class);
|
||||
assertSame(bf.getBean("store1"), nb.getDoubleStore());
|
||||
assertSame(bf.getBean("store2"), nb.getFloatStore());
|
||||
|
||||
String[] numberStoreNames = bf.getBeanNamesForType(ResolvableType.forClass(NumberStore.class));
|
||||
String[] doubleStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Double.class));
|
||||
String[] floatStoreNames = bf.getBeanNamesForType(ResolvableType.forClassWithGenerics(NumberStore.class, Float.class));
|
||||
assertEquals(2, numberStoreNames.length);
|
||||
assertEquals("store1", numberStoreNames[0]);
|
||||
assertEquals("store2", numberStoreNames[1]);
|
||||
assertEquals(1, doubleStoreNames.length);
|
||||
assertEquals("store1", doubleStoreNames[0]);
|
||||
assertEquals(1, floatStoreNames.length);
|
||||
assertEquals("store2", floatStoreNames[0]);
|
||||
}
|
||||
|
||||
|
||||
@@ -851,6 +887,14 @@ public class BeanFactoryGenericsTests {
|
||||
}
|
||||
|
||||
|
||||
public static class DoubleStore extends NumberStore<Double> {
|
||||
}
|
||||
|
||||
|
||||
public static class FloatStore extends NumberStore<Float> {
|
||||
}
|
||||
|
||||
|
||||
public static class NumberBean {
|
||||
|
||||
private final NumberStore<Double> doubleStore;
|
||||
|
||||
Reference in New Issue
Block a user