Search in sources :

Example 1 with ConfigurationClassPostProcessor

use of org.springframework.context.annotation.ConfigurationClassPostProcessor in project spring-framework by spring-projects.

the class ConfigurationClassProcessingTests method initBeanFactory.

/**
	 * Creates a new {@link BeanFactory}, populates it with a {@link BeanDefinition}
	 * for each of the given {@link Configuration} {@code configClasses}, and then
	 * post-processes the factory using JavaConfig's {@link ConfigurationClassPostProcessor}.
	 * When complete, the factory is ready to service requests for any {@link Bean} methods
	 * declared by {@code configClasses}.
	 */
private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    for (Class<?> configClass : configClasses) {
        String configBeanName = configClass.getName();
        factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
    }
    ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
    ccpp.postProcessBeanDefinitionRegistry(factory);
    ccpp.postProcessBeanFactory(factory);
    RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor();
    rapp.setBeanFactory(factory);
    factory.addBeanPostProcessor(rapp);
    factory.freezeConfiguration();
    return factory;
}
Also used : RequiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor)

Example 2 with ConfigurationClassPostProcessor

use of org.springframework.context.annotation.ConfigurationClassPostProcessor in project spring-framework by spring-projects.

the class ConfigurationClassAspectIntegrationTests method assertAdviceWasApplied.

private void assertAdviceWasApplied(Class<?> configClass) {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource("aspectj-autoproxy-config.xml", ConfigurationClassAspectIntegrationTests.class));
    GenericApplicationContext ctx = new GenericApplicationContext(factory);
    ctx.addBeanFactoryPostProcessor(new ConfigurationClassPostProcessor());
    ctx.registerBeanDefinition("config", new RootBeanDefinition(configClass));
    ctx.refresh();
    TestBean testBean = ctx.getBean("testBean", TestBean.class);
    assertThat(testBean.getName(), equalTo("name"));
    testBean.absquatulate();
    assertThat(testBean.getName(), equalTo("advisedName"));
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 3 with ConfigurationClassPostProcessor

use of org.springframework.context.annotation.ConfigurationClassPostProcessor in project spring-framework by spring-projects.

the class ImportTests method processConfigurationClasses.

private DefaultListableBeanFactory processConfigurationClasses(Class<?>... classes) {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    for (Class<?> clazz : classes) {
        beanFactory.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
    }
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(beanFactory);
    return beanFactory;
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor)

Example 4 with ConfigurationClassPostProcessor

use of org.springframework.context.annotation.ConfigurationClassPostProcessor in project spring-framework by spring-projects.

the class BeanAnnotationAttributePropagationTests method beanDef.

private AbstractBeanDefinition beanDef(Class<?> configClass) {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(factory);
    return (AbstractBeanDefinition) factory.getBeanDefinition("foo");
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor)

Example 5 with ConfigurationClassPostProcessor

use of org.springframework.context.annotation.ConfigurationClassPostProcessor in project spring-framework by spring-projects.

the class ImportTests method testProcessImportsWithAsm.

@Test
public void testProcessImportsWithAsm() {
    int configClasses = 2;
    int beansInClasses = 2;
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ConfigurationWithImportAnnotation.class.getName()));
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(beanFactory);
    assertThat(beanFactory.getBeanDefinitionCount(), equalTo(configClasses + beansInClasses));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)6 ConfigurationClassPostProcessor (org.springframework.context.annotation.ConfigurationClassPostProcessor)6 Test (org.junit.Test)2 RequiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 TestBean (org.springframework.tests.sample.beans.TestBean)1