Search in sources :

Example 1 with NestedTestBean

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

the class DefaultListableBeanFactoryTests method testBeanDefinitionOverridingWithAlias.

@Test
public void testBeanDefinitionOverridingWithAlias() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
    lbf.registerAlias("test", "testAlias");
    lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
    lbf.registerAlias("test", "testAlias");
    assertTrue(lbf.getBean("test") instanceof NestedTestBean);
    assertTrue(lbf.getBean("testAlias") instanceof NestedTestBean);
}
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) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 2 with NestedTestBean

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

the class DefaultListableBeanFactoryTests method testReregisterBeanDefinition.

@Test
public void testReregisterBeanDefinition() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    lbf.registerBeanDefinition("testBean", bd1);
    assertTrue(lbf.getBean("testBean") instanceof TestBean);
    RootBeanDefinition bd2 = new RootBeanDefinition(NestedTestBean.class);
    bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    lbf.registerBeanDefinition("testBean", bd2);
    assertTrue(lbf.getBean("testBean") instanceof NestedTestBean);
}
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) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 3 with NestedTestBean

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

the class DefaultListableBeanFactoryTests method testBeanDefinitionOverriding.

@Test
public void testBeanDefinitionOverriding() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
    lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
    lbf.registerAlias("otherTest", "test2");
    lbf.registerAlias("test", "test2");
    assertTrue(lbf.getBean("test") instanceof NestedTestBean);
    assertTrue(lbf.getBean("test2") instanceof NestedTestBean);
}
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) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 4 with NestedTestBean

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

the class InjectAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithAtRequired.

@Test
public void testExtendedResourceInjectionWithAtRequired() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerResolvableDependency(BeanFactory.class, bf);
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
    RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    NestedTestBean ntb = new NestedTestBean();
    bf.registerSingleton("nestedTestBean", ntb);
    TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertSame(tb, bean.getTestBean());
    assertSame(tb, bean.getTestBean2());
    assertSame(tb, bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertSame(ntb, bean.getNestedTestBean());
    assertSame(bf, bean.getBeanFactory());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 5 with NestedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithNull.

@Test
public void testConstructorResourceInjectionWithNull() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerResolvableDependency(BeanFactory.class, bf);
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NullNestedTestBeanFactoryBean.class));
    bf.registerSingleton("nestedTestBean2", new NestedTestBean());
    ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
    assertSame(tb, bean.getTestBean());
    assertSame(tb, bean.getTestBean2());
    assertSame(tb, bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertNull(bean.getNestedTestBean());
    assertSame(bf, bean.getBeanFactory());
    bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
    assertSame(tb, bean.getTestBean());
    assertSame(tb, bean.getTestBean2());
    assertSame(tb, bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertNull(bean.getNestedTestBean());
    assertSame(bf, bean.getBeanFactory());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 ITestBean (org.springframework.tests.sample.beans.ITestBean)7 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)7 TestBean (org.springframework.tests.sample.beans.TestBean)7 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)4 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)3