Search in sources :

Example 1 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByNameWithNoDependencyCheck.

@Test
public void testAutowireBeanByNameWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertNull(bean.getSpouse());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 2 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByName.

@Test
public void testAutowireBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(spouse, bean.getSpouse());
    assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 3 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority.

@Test
public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    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(), equalTo(lbf.getBean("spouse")));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 4 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByTypeWithNoDependencyCheck.

@Test
public void testAutowireBeanByTypeWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    assertNull(bean.getSpouse());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 5 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByTypeWithTwoMatchesAndOnePrimary.

@Test
public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPrimary(true);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.registerBeanDefinition("spouse", bd2);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertThat(bean.getSpouse(), equalTo(lbf.getBean("test")));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)16 DependenciesBean (org.springframework.tests.sample.beans.DependenciesBean)16 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)12 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)6 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2