Search in sources :

Example 1 with InitializationBeanPostProcessor

use of cn.taketoday.beans.factory.InitializationBeanPostProcessor in project today-infrastructure by TAKETODAY.

the class AnnoTestBean method nullReturningBeanPostProcessor.

@Test
void nullReturningBeanPostProcessor() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AutowiredConfig.class);
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return (bean instanceof AnnoTestBean ? null : bean);
        }
    });
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }
    });
    context.refresh();
}
Also used : InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) Test(org.junit.jupiter.api.Test)

Example 2 with InitializationBeanPostProcessor

use of cn.taketoday.beans.factory.InitializationBeanPostProcessor in project today-framework by TAKETODAY.

the class StandardApplicationContextTests method nullReturningBeanPostProcessor.

@Test
void nullReturningBeanPostProcessor() {
    StandardApplicationContext context = new StandardApplicationContext();
    context.register(AutowiredConfig.class);
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return (bean instanceof TestBean ? null : bean);
        }
    });
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }
    });
    context.refresh();
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) Test(org.junit.jupiter.api.Test)

Example 3 with InitializationBeanPostProcessor

use of cn.taketoday.beans.factory.InitializationBeanPostProcessor in project today-framework by TAKETODAY.

the class XmlListableBeanFactoryTests method setup.

@BeforeEach
public void setup() {
    parent = new StandardBeanFactory();
    Map map = new HashMap();
    map.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("father", bd1);
    map = new HashMap();
    map.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new StandardBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
// this.factory.preInstantiateSingletons();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) HashMap(java.util.HashMap) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DummyFactory(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) LifecycleBean(cn.taketoday.beans.testfixture.beans.LifecycleBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(cn.taketoday.beans.BeansException) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with InitializationBeanPostProcessor

use of cn.taketoday.beans.factory.InitializationBeanPostProcessor in project today-framework by TAKETODAY.

the class AnnoTestBean method nullReturningBeanPostProcessor.

@Test
void nullReturningBeanPostProcessor() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AutowiredConfig.class);
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return (bean instanceof AnnoTestBean ? null : bean);
        }
    });
    context.getBeanFactory().addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            bean.getClass().getName();
            return bean;
        }
    });
    context.refresh();
}
Also used : InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) Test(org.junit.jupiter.api.Test)

Example 5 with InitializationBeanPostProcessor

use of cn.taketoday.beans.factory.InitializationBeanPostProcessor in project today-infrastructure by TAKETODAY.

the class XmlListableBeanFactoryTests method setup.

@BeforeEach
public void setup() {
    parent = new StandardBeanFactory();
    Map map = new HashMap();
    map.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("father", bd1);
    map = new HashMap();
    map.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new StandardBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
// this.factory.preInstantiateSingletons();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) HashMap(java.util.HashMap) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DummyFactory(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) LifecycleBean(cn.taketoday.beans.testfixture.beans.LifecycleBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(cn.taketoday.beans.BeansException) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

InitializationBeanPostProcessor (cn.taketoday.beans.factory.InitializationBeanPostProcessor)5 Test (org.junit.jupiter.api.Test)3 BeansException (cn.taketoday.beans.BeansException)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 LifecycleBean (cn.taketoday.beans.testfixture.beans.LifecycleBean)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 DummyFactory (cn.taketoday.beans.testfixture.beans.factory.DummyFactory)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)1