Search in sources :

Example 76 with DefaultListableBeanFactory

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

the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithMultipleCandidatesAsCollection.

@Test
public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    NestedTestBean ntb2 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    assertNull(bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertEquals(2, bean.getNestedTestBeans().size());
    assertSame(ntb1, bean.getNestedTestBeans().get(0));
    assertSame(ntb2, bean.getNestedTestBeans().get(1));
    bf.destroySingletons();
}
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 77 with DefaultListableBeanFactory

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

the class AutowiredAnnotationBeanPostProcessorTests method testBeanAutowiredWithFactoryBean.

/**
	 * Verifies that a dependency on a {@link FactoryBean} can be autowired via
	 * {@link Autowired @Autowired}, specifically addressing the JIRA issue
	 * raised in <a
	 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-4040"
	 * target="_blank">SPR-4040</a>.
	 */
@Test
public void testBeanAutowiredWithFactoryBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("factoryBeanDependentBean", new RootBeanDefinition(FactoryBeanDependentBean.class));
    bf.registerSingleton("stringFactoryBean", new StringFactoryBean());
    final StringFactoryBean factoryBean = (StringFactoryBean) bf.getBean("&stringFactoryBean");
    final FactoryBeanDependentBean bean = (FactoryBeanDependentBean) bf.getBean("factoryBeanDependentBean");
    assertNotNull("The singleton StringFactoryBean should have been registered.", factoryBean);
    assertNotNull("The factoryBeanDependentBean should have been registered.", bean);
    assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", factoryBean, bean.getFactoryBean());
    bf.destroySingletons();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 78 with DefaultListableBeanFactory

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

the class InjectAnnotationBeanPostProcessorTests method testOptionalFieldInjectionWithBeanAvailable.

@Test
public void testOptionalFieldInjectionWithBeanAvailable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class));
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean");
    assertTrue(bean.getTestBean().isPresent());
    assertSame(bf.getBean("testBean"), bean.getTestBean().get());
    bf.destroySingletons();
}
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 79 with DefaultListableBeanFactory

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

the class InjectAnnotationBeanPostProcessorTests method testNullableMethodInjectionWithBeanNotAvailable.

@Test
public void testNullableMethodInjectionWithBeanNotAvailable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableMethodInjectionBean.class));
    NullableMethodInjectionBean bean = (NullableMethodInjectionBean) bf.getBean("annotatedBean");
    assertNull(bean.getTestBean());
    bf.destroySingletons();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 80 with DefaultListableBeanFactory

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

the class InjectAnnotationBeanPostProcessorTests method testBeanAutowiredWithFactoryBean.

/**
	 * Verifies that a dependency on a {@link org.springframework.beans.factory.FactoryBean} can be autowired via
	 * {@link org.springframework.beans.factory.annotation.Autowired @Inject}, specifically addressing the JIRA issue
	 * raised in <a
	 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-4040"
	 * target="_blank">SPR-4040</a>.
	 */
@Test
public void testBeanAutowiredWithFactoryBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("factoryBeanDependentBean", new RootBeanDefinition(FactoryBeanDependentBean.class));
    bf.registerSingleton("stringFactoryBean", new StringFactoryBean());
    final StringFactoryBean factoryBean = (StringFactoryBean) bf.getBean("&stringFactoryBean");
    final FactoryBeanDependentBean bean = (FactoryBeanDependentBean) bf.getBean("factoryBeanDependentBean");
    assertNotNull("The singleton StringFactoryBean should have been registered.", factoryBean);
    assertNotNull("The factoryBeanDependentBean should have been registered.", bean);
    assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", factoryBean, bean.getFactoryBean());
    bf.destroySingletons();
}
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