Search in sources :

Example 16 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project HotswapAgent by HotswapProjects.

the class ResetBeanPostProcessorCaches method reset.

/**
 * Reset all post processors associated with a bean factory.
 *
 * @param beanFactory beanFactory to use
 */
public static void reset(DefaultListableBeanFactory beanFactory) {
    Class<?> c = getReflectionUtilsClassOrNull();
    if (c != null) {
        try {
            Method m = c.getDeclaredMethod("clearCache");
            m.invoke(c);
        } catch (Exception version42Failed) {
            try {
                // spring 4.0.x, 4.1.x without clearCache method, clear manually
                Field declaredMethodsCache = c.getDeclaredField("declaredMethodsCache");
                declaredMethodsCache.setAccessible(true);
                ((Map) declaredMethodsCache.get(null)).clear();
                Field declaredFieldsCache = c.getDeclaredField("declaredFieldsCache");
                declaredFieldsCache.setAccessible(true);
                ((Map) declaredFieldsCache.get(null)).clear();
            } catch (Exception version40Failed) {
                LOGGER.debug("Failed to clear internal method/field cache, it's normal with spring 4.1x or lower", version40Failed);
            }
        }
        LOGGER.trace("Cleared Spring 4.2+ internal method/field cache.");
    }
    for (BeanPostProcessor bpp : beanFactory.getBeanPostProcessors()) {
        if (bpp instanceof AutowiredAnnotationBeanPostProcessor) {
            resetAutowiredAnnotationBeanPostProcessorCache((AutowiredAnnotationBeanPostProcessor) bpp);
        } else if (bpp instanceof InitDestroyAnnotationBeanPostProcessor) {
            resetInitDestroyAnnotationBeanPostProcessorCache((InitDestroyAnnotationBeanPostProcessor) bpp);
        }
    }
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) InitDestroyAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor) Field(java.lang.reflect.Field) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InitDestroyAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor) AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) Method(java.lang.reflect.Method)

Example 17 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method beanPostProcessorWithWrappedObjectAndCloseable.

@Test
void beanPostProcessorWithWrappedObjectAndCloseable() {
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithCloseable.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return new TestBean();
        }
    });
    BeanWithDisposableBean.closed = false;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertThat(BeanWithCloseable.closed).as("Destroy method invoked").isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 18 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method beanPostProcessorWithWrappedObjectAndDisposableBean.

@Test
void beanPostProcessorWithWrappedObjectAndDisposableBean() {
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return new TestBean();
        }
    });
    BeanWithDisposableBean.closed = false;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertThat(BeanWithDisposableBean.closed).as("Destroy method invoked").isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 19 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class TestBean method nullReturningBeanPostProcessor.

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

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

        @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 : BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) Test(org.junit.jupiter.api.Test)

Example 20 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class XmlListableBeanFactoryTests method setup.

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

        @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 : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(org.springframework.beans.BeansException) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)20 InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)6 Test (org.junit.jupiter.api.Test)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)4 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 Test (org.junit.Test)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)3 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)3 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)3 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2