Search in sources :

Example 6 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireWithSatisfiedJavaBeanDependency.

@Test
public void testAutowireWithSatisfiedJavaBeanDependency() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Rod");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("rod", bd);
    assertEquals(1, lbf.getBeanDefinitionCount());
    // Depends on age, name and spouse (TestBean)
    Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertEquals(1, lbf.getBeanDefinitionCount());
    DependenciesBean kerry = (DependenciesBean) registered;
    TestBean rod = (TestBean) lbf.getBean("rod");
    assertSame(rod, kerry.getSpouse());
}
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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) 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 7 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByType.

@Test
public void testAutowireExistingBeanByType() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("test", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    TestBean test = (TestBean) lbf.getBean("test");
    assertEquals(existingBean.getSpouse(), test);
}
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 8 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByTypeWithTwoMatchesAndPriority.

@Test
public void testAutowireBeanByTypeWithTwoMatchesAndPriority() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    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(), 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)

Example 9 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByNameWithNoDependencyCheck.

@Test
public void testAutowireExistingBeanByNameWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertNull(existingBean.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 10 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByType.

@Test
public void testAutowireBeanByType() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("test", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    TestBean test = (TestBean) lbf.getBean("test");
    assertEquals(test, bean.getSpouse());
}
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)

Aggregations

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