Search in sources :

Example 46 with DefaultListableBeanFactory

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

the class RequiredAnnotationBeanPostProcessorTests method testWithFactoryBean.

@Test
public void testWithFactoryBean() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
    beanDef.setFactoryBeanName("testBeanFactory");
    beanDef.setFactoryMethodName("create");
    factory.registerBeanDefinition("testBean", beanDef);
    factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
    RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(factory);
    factory.addBeanPostProcessor(bpp);
    factory.preInstantiateSingletons();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 47 with DefaultListableBeanFactory

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

the class RequiredAnnotationBeanPostProcessorTests method testWithCustomAnnotation.

@Test
public void testWithCustomAnnotation() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor();
        rabpp.setRequiredAnnotationType(MyRequired.class);
        factory.addBeanPostProcessor(rabpp);
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Property"));
        assertTrue(message.contains("name"));
        assertTrue(message.contains("testBean"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 48 with DefaultListableBeanFactory

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

the class RequiredAnnotationBeanPostProcessorTests method testWithAllRequiredPropertiesSpecified.

@Test
public void testWithAllRequiredPropertiesSpecified() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
    factory.registerBeanDefinition("testBean", beanDef);
    factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
    factory.preInstantiateSingletons();
    RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
    assertEquals(24, bean.getAge());
    assertEquals("Blue", bean.getFavouriteColour());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 49 with DefaultListableBeanFactory

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

the class RequiredAnnotationBeanPostProcessorTests method testWithStaticFactoryMethodAndRequiredPropertiesSpecified.

@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).setFactoryMethod("create").addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
    factory.registerBeanDefinition("testBean", beanDef);
    factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
    factory.preInstantiateSingletons();
    RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
    assertEquals(24, bean.getAge());
    assertEquals("Blue", bean.getFavouriteColour());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 50 with DefaultListableBeanFactory

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

the class DeprecatedBeanWarnerTests method postProcess.

@Test
@SuppressWarnings("deprecation")
public void postProcess() {
    beanFactory = new DefaultListableBeanFactory();
    BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class);
    String beanName = "deprecated";
    beanFactory.registerBeanDefinition(beanName, def);
    warner = new MyDeprecatedBeanWarner();
    warner.postProcessBeanFactory(beanFactory);
    assertEquals(beanName, this.beanName);
    assertEquals(def, this.beanDefinition);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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