Search in sources :

Example 1 with InitDestroyAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor 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 2 with InitDestroyAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessorTests method testPostConstructAndPreDestroyWithManualConfiguration.

@Test
public void testPostConstructAndPreDestroyWithManualConfiguration() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
    bpp.setInitAnnotationType(PostConstruct.class);
    bpp.setDestroyAnnotationType(PreDestroy.class);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
    AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
}
Also used : InitDestroyAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with InitDestroyAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessorTests method testSerializationWithManualConfiguration.

@Test
public void testSerializationWithManualConfiguration() throws Exception {
    InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
    bpp.setInitAnnotationType(PostConstruct.class);
    bpp.setDestroyAnnotationType(PreDestroy.class);
    InitDestroyAnnotationBeanPostProcessor bpp2 = SerializationTestUtils.serializeAndDeserialize(bpp);
    AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
    bpp2.postProcessBeforeDestruction(bean, "annotatedBean");
    assertThat(bean.destroyCalled).isTrue();
}
Also used : InitDestroyAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor) Test(org.junit.jupiter.api.Test)

Aggregations

InitDestroyAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor)3 Test (org.junit.jupiter.api.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1