Search in sources :

Example 11 with DependenciesBean

use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method registerExistingSingletonWithAutowire.

@Test
void registerExistingSingletonWithAutowire() {
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Tony");
    pvs.add("age", "48");
    RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class);
    bd.setPropertyValues(pvs);
    bd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
    lbf.registerBeanDefinition("test", bd);
    Object singletonObject = new TestBean();
    lbf.registerSingleton("singletonObject", singletonObject);
    assertThat(lbf.containsBean("singletonObject")).isTrue();
    assertThat(lbf.isSingleton("singletonObject")).isTrue();
    assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class);
    assertThat(lbf.getAliases("singletonObject").length).isEqualTo(0);
    DependenciesBean test = (DependenciesBean) lbf.getBean("test");
    assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject);
    assertThat(test.getSpouse()).isEqualTo(singletonObject);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 12 with DependenciesBean

use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method autowireExistingBeanByNameWithNoDependencyCheck.

@Test
void autowireExistingBeanByNameWithNoDependencyCheck() {
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertThat(existingBean.getSpouse()).isNull();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 13 with DependenciesBean

use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method autowireBeanByTypeWithTwoMatchesAndPriority.

@Test
void autowireBeanByTypeWithTwoMatchesAndPriority() {
    lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.registerBeanDefinition("spouse", bd2);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("test"));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 14 with DependenciesBean

use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method autowireBeanByTypePrimaryTakesPrecedenceOverPriority.

@Test
void autowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
    lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPrimary(true);
    lbf.registerBeanDefinition("test", bd);
    lbf.registerBeanDefinition("spouse", bd2);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("spouse"));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 15 with DependenciesBean

use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method autowireExistingBeanByNameWithDependencyCheck.

@Test
void autowireExistingBeanByNameWithDependencyCheck() {
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Aggregations

DependenciesBean (org.springframework.beans.testfixture.beans.DependenciesBean)17 Test (org.junit.jupiter.api.Test)16 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)12 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)7 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 FatalBeanException (org.springframework.beans.FatalBeanException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1