Search in sources :

Example 16 with DependenciesBean

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

the class XmlBeanFactoryTests method doTestAutowire.

private void doTestAutowire(DefaultListableBeanFactory xbf) throws Exception {
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    TestBean kerry = (TestBean) xbf.getBean("spouse");
    // should have been autowired
    assertEquals(kerry, rod1.getSpouse());
    DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
    // should have been autowired
    assertEquals(kerry, rod1a.getSpouse());
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertEquals(kerry, rod2.getSpouse());
    DependenciesBean rod2a = (DependenciesBean) xbf.getBean("rod2a");
    // should have been set explicitly
    assertEquals(kerry, rod2a.getSpouse());
    ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertEquals(kerry, rod3.getSpouse1());
    assertEquals(kerry, rod3.getSpouse2());
    assertEquals(other, rod3.getOther());
    ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
    // should have been autowired
    assertEquals(kerry, rod3a.getSpouse1());
    assertEquals(kerry, rod3a.getSpouse2());
    assertEquals(other, rod3a.getOther());
    try {
        xbf.getBean("rod4", ConstructorDependenciesBean.class);
        fail("Must have thrown a FatalBeanException");
    } catch (FatalBeanException expected) {
    // expected
    }
    DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
    // Should not have been autowired
    assertNull(rod5.getSpouse());
    BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
    assertTrue(appCtx.containsBean("rod1"));
    assertTrue(appCtx.containsBean("jenny"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean)

Example 17 with DependenciesBean

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

the class XmlBeanFactoryTests method testAutowireWithDefault.

@Test
public void testAutowireWithDefault() throws Exception {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_AUTOWIRE_CONTEXT);
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    // should have been autowired
    assertNotNull(rod1.getSpouse());
    assertTrue(rod1.getSpouse().getName().equals("Kerry"));
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertNotNull(rod2.getSpouse());
    assertTrue(rod2.getSpouse().getName().equals("Kerry"));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) 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