Search in sources :

Example 11 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByTypeWithDependencyCheck.

@Test
public void testAutowireExistingBeanByTypeWithDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DependenciesBean existingBean = new DependenciesBean();
    try {
        lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
    }
}
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 12 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testRegisterExistingSingletonWithAutowire.

@Test
public void testRegisterExistingSingletonWithAutowire() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    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);
    assertTrue(lbf.containsBean("singletonObject"));
    assertTrue(lbf.isSingleton("singletonObject"));
    assertEquals(TestBean.class, lbf.getType("singletonObject"));
    assertEquals(0, lbf.getAliases("singletonObject").length);
    DependenciesBean test = (DependenciesBean) lbf.getBean("test");
    assertEquals(singletonObject, lbf.getBean("singletonObject"));
    assertEquals(singletonObject, test.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 13 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByName.

@Test
public void testAutowireExistingBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(existingBean.getSpouse(), spouse);
    assertSame(spouse, BeanFactoryUtils.beanOfType(lbf, TestBean.class));
}
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 14 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByNameWithDependencyCheck.

@Test
public void testAutowireExistingBeanByNameWithDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    try {
        lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
    // expected
    }
}
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 15 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method testAutowireExistingBeanByTypeWithNoDependencyCheck.

@Test
public void testAutowireExistingBeanByTypeWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    assertNull(existingBean.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)

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