|
|
|
@ -65,6 +65,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
|
|
|
|
|
import org.springframework.beans.factory.support.ChildBeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
|
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.support.ManagedList;
|
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
|
|
|
|
@ -1370,10 +1371,10 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("rod2", bd2);
|
|
|
|
|
lbf.setParameterNameDiscoverer(new DefaultParameterNameDiscoverer());
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() ->
|
|
|
|
|
lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false))
|
|
|
|
|
.withMessageContaining("rod")
|
|
|
|
|
.withMessageContaining("rod2");
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false))
|
|
|
|
|
.withMessageContaining("rod")
|
|
|
|
|
.withMessageContaining("rod2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -1432,6 +1433,57 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
assertThat(bean.getSpouse()).isNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void autowirePreferredConstructors() {
|
|
|
|
|
lbf.registerBeanDefinition("spouse1", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
lbf.registerBeanDefinition("spouse2", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependenciesBean.class);
|
|
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
|
|
|
|
|
lbf.registerBeanDefinition("bean", bd);
|
|
|
|
|
lbf.setParameterNameDiscoverer(new DefaultParameterNameDiscoverer());
|
|
|
|
|
|
|
|
|
|
ConstructorDependenciesBean bean = lbf.getBean(ConstructorDependenciesBean.class);
|
|
|
|
|
Object spouse1 = lbf.getBean("spouse1");
|
|
|
|
|
Object spouse2 = lbf.getBean("spouse2");
|
|
|
|
|
assertThat(bean.getSpouse1()).isSameAs(spouse1);
|
|
|
|
|
assertThat(bean.getSpouse2()).isSameAs(spouse2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void autowirePreferredConstructorsFromAttribute() {
|
|
|
|
|
lbf.registerBeanDefinition("spouse1", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
lbf.registerBeanDefinition("spouse2", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
GenericBeanDefinition bd = new GenericBeanDefinition();
|
|
|
|
|
bd.setBeanClass(ConstructorDependenciesBean.class);
|
|
|
|
|
bd.setAttribute(GenericBeanDefinition.PREFERRED_CONSTRUCTORS_ATTRIBUTE,
|
|
|
|
|
ConstructorDependenciesBean.class.getConstructors());
|
|
|
|
|
lbf.registerBeanDefinition("bean", bd);
|
|
|
|
|
lbf.setParameterNameDiscoverer(new DefaultParameterNameDiscoverer());
|
|
|
|
|
|
|
|
|
|
ConstructorDependenciesBean bean = lbf.getBean(ConstructorDependenciesBean.class);
|
|
|
|
|
Object spouse1 = lbf.getBean("spouse1");
|
|
|
|
|
Object spouse2 = lbf.getBean("spouse2");
|
|
|
|
|
assertThat(bean.getSpouse1()).isSameAs(spouse1);
|
|
|
|
|
assertThat(bean.getSpouse2()).isSameAs(spouse2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void autowirePreferredConstructorFromAttribute() throws Exception {
|
|
|
|
|
lbf.registerBeanDefinition("spouse1", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
lbf.registerBeanDefinition("spouse2", new RootBeanDefinition(TestBean.class));
|
|
|
|
|
GenericBeanDefinition bd = new GenericBeanDefinition();
|
|
|
|
|
bd.setBeanClass(ConstructorDependenciesBean.class);
|
|
|
|
|
bd.setAttribute(GenericBeanDefinition.PREFERRED_CONSTRUCTORS_ATTRIBUTE,
|
|
|
|
|
ConstructorDependenciesBean.class.getConstructor(TestBean.class));
|
|
|
|
|
lbf.registerBeanDefinition("bean", bd);
|
|
|
|
|
lbf.setParameterNameDiscoverer(new DefaultParameterNameDiscoverer());
|
|
|
|
|
|
|
|
|
|
ConstructorDependenciesBean bean = lbf.getBean(ConstructorDependenciesBean.class);
|
|
|
|
|
Object spouse = lbf.getBean("spouse1");
|
|
|
|
|
assertThat(bean.getSpouse1()).isSameAs(spouse);
|
|
|
|
|
assertThat(bean.getSpouse2()).isNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void dependsOnCycle() {
|
|
|
|
|
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
|
|
|
|
@ -1441,11 +1493,11 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
bd2.setDependsOn("tb1");
|
|
|
|
|
lbf.registerBeanDefinition("tb2", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
|
|
|
|
|
lbf.preInstantiateSingletons())
|
|
|
|
|
.withMessageContaining("Circular")
|
|
|
|
|
.withMessageContaining("'tb2'")
|
|
|
|
|
.withMessageContaining("'tb1'");
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.preInstantiateSingletons())
|
|
|
|
|
.withMessageContaining("Circular")
|
|
|
|
|
.withMessageContaining("'tb2'")
|
|
|
|
|
.withMessageContaining("'tb1'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -1460,11 +1512,11 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
bd3.setDependsOn("tb1");
|
|
|
|
|
lbf.registerBeanDefinition("tb3", bd3);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
|
|
|
|
|
lbf::preInstantiateSingletons)
|
|
|
|
|
.withMessageContaining("Circular")
|
|
|
|
|
.withMessageContaining("'tb3'")
|
|
|
|
|
.withMessageContaining("'tb1'");
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class)
|
|
|
|
|
.isThrownBy(lbf::preInstantiateSingletons)
|
|
|
|
|
.withMessageContaining("Circular")
|
|
|
|
|
.withMessageContaining("'tb3'")
|
|
|
|
|
.withMessageContaining("'tb1'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -1562,9 +1614,9 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("bd1", bd1);
|
|
|
|
|
lbf.registerBeanDefinition("bd2", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() ->
|
|
|
|
|
lbf.getBean(TestBean.class))
|
|
|
|
|
.withMessageContaining("more than one 'primary'");
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.getBean(TestBean.class))
|
|
|
|
|
.withMessageContaining("more than one 'primary'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -1607,10 +1659,10 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("bd1", bd1);
|
|
|
|
|
lbf.registerBeanDefinition("bd2", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() ->
|
|
|
|
|
lbf.getBean(TestBean.class))
|
|
|
|
|
.withMessageContaining("Multiple beans found with the same priority")
|
|
|
|
|
.withMessageContaining("5"); // conflicting priority
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.getBean(TestBean.class))
|
|
|
|
|
.withMessageContaining("Multiple beans found with the same priority")
|
|
|
|
|
.withMessageContaining("5"); // conflicting priority
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -1815,9 +1867,9 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("bd1", bd1);
|
|
|
|
|
lbf.registerBeanDefinition("bd2", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() ->
|
|
|
|
|
lbf.getBean(ConstructorDependency.class, 42))
|
|
|
|
|
.withMessageContaining("more than one 'primary'");
|
|
|
|
|
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.getBean(ConstructorDependency.class, 42))
|
|
|
|
|
.withMessageContaining("more than one 'primary'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -2004,10 +2056,10 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("test", bd);
|
|
|
|
|
lbf.registerBeanDefinition("spouse", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() ->
|
|
|
|
|
lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true))
|
|
|
|
|
.withMessageContaining("test")
|
|
|
|
|
.withMessageContaining("spouse");
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true))
|
|
|
|
|
.withMessageContaining("test")
|
|
|
|
|
.withMessageContaining("spouse");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -2071,10 +2123,10 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
lbf.registerBeanDefinition("test", bd);
|
|
|
|
|
lbf.registerBeanDefinition("spouse", bd2);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() ->
|
|
|
|
|
lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true))
|
|
|
|
|
.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class)
|
|
|
|
|
.withMessageContaining("5");
|
|
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true))
|
|
|
|
|
.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class)
|
|
|
|
|
.withMessageContaining("5");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -2337,20 +2389,20 @@ class DefaultListableBeanFactoryTests {
|
|
|
|
|
void beanDefinitionWithInterface() {
|
|
|
|
|
lbf.registerBeanDefinition("test", new RootBeanDefinition(ITestBean.class));
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
|
|
|
|
|
lbf.getBean("test"))
|
|
|
|
|
.withMessageContaining("interface")
|
|
|
|
|
.satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test"));
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.getBean("test"))
|
|
|
|
|
.withMessageContaining("interface")
|
|
|
|
|
.satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void beanDefinitionWithAbstractClass() {
|
|
|
|
|
lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class));
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
|
|
|
|
|
lbf.getBean("test"))
|
|
|
|
|
.withMessageContaining("abstract")
|
|
|
|
|
.satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test"));
|
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class)
|
|
|
|
|
.isThrownBy(() -> lbf.getBean("test"))
|
|
|
|
|
.withMessageContaining("abstract")
|
|
|
|
|
.satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo("test"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|