Search in sources :

Example 81 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testConstructorInjectionWithMap.

@Test
public void testConstructorInjectionWithMap() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    TestBean tb1 = new TestBean();
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean1", tb1);
    bf.registerSingleton("testBean2", tb1);
    MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
    assertEquals(2, bean.getTestBeanMap().size());
    assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
    assertTrue(bean.getTestBeanMap().keySet().contains("testBean2"));
    assertTrue(bean.getTestBeanMap().values().contains(tb1));
    assertTrue(bean.getTestBeanMap().values().contains(tb2));
    bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean");
    assertEquals(2, bean.getTestBeanMap().size());
    assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
    assertTrue(bean.getTestBeanMap().keySet().contains("testBean2"));
    assertTrue(bean.getTestBeanMap().values().contains(tb1));
    assertTrue(bean.getTestBeanMap().values().contains(tb2));
}
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) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 82 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testExtendedResourceInjection.

@Test
public void testExtendedResourceInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerResolvableDependency(BeanFactory.class, bf);
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    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());
    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 83 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.

the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjection.

@Test
public void testConstructorResourceInjection() {
    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);
    NestedTestBean ntb = new NestedTestBean();
    bf.registerSingleton("nestedTestBean", ntb);
    ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) 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());
    bean = (ConstructorResourceInjectionBean) 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 84 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.

the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithNoCandidatesAndNoFallback.

@Test
public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class));
    try {
        bf.getBean("annotatedBean");
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(ConstructorWithoutFallbackBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
    }
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 85 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.

the class AutowiredAnnotationBeanPostProcessorTests method testFactoryBeanSelfInjectionViaFactoryMethod.

// SPR-15125
@Test
public void testFactoryBeanSelfInjectionViaFactoryMethod() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(SelfInjectingFactoryBean.class);
    bd.setFactoryMethodName("create");
    bf.registerBeanDefinition("annotatedBean", bd);
    SelfInjectingFactoryBean bean = bf.getBean(SelfInjectingFactoryBean.class);
    assertSame(bf.getBean("annotatedBean"), bean.testBean);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)652 Test (org.junit.Test)576 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)317 TestBean (org.springframework.tests.sample.beans.TestBean)249 ITestBean (org.springframework.tests.sample.beans.ITestBean)199 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)152 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)101 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)96 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)82 ClassPathResource (org.springframework.core.io.ClassPathResource)80 Properties (java.util.Properties)32 Before (org.junit.Before)27 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)26 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)24 HashMap (java.util.HashMap)22 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)22 DependenciesBean (org.springframework.tests.sample.beans.DependenciesBean)17 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)14