org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor Java Examples

The following examples show how to use org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
	assertTrue(bean.initCalled);
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
}
 
Example #2
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSerializationWithManualConfiguration() throws Exception {
	InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
	bpp.setInitAnnotationType(PostConstruct.class);
	bpp.setDestroyAnnotationType(PreDestroy.class);
	InitDestroyAnnotationBeanPostProcessor bpp2 = (InitDestroyAnnotationBeanPostProcessor)
			SerializationTestUtils.serializeAndDeserialize(bpp);

	AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
	bpp2.postProcessBeforeDestruction(bean, "annotatedBean");
	assertTrue(bean.destroyCalled);
}
 
Example #3
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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");
	assertTrue(bean.initCalled);
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
}
 
Example #4
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSerializationWithManualConfiguration() throws Exception {
	InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
	bpp.setInitAnnotationType(PostConstruct.class);
	bpp.setDestroyAnnotationType(PreDestroy.class);
	InitDestroyAnnotationBeanPostProcessor bpp2 = (InitDestroyAnnotationBeanPostProcessor)
			SerializationTestUtils.serializeAndDeserialize(bpp);

	AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
	bpp2.postProcessBeforeDestruction(bean, "annotatedBean");
	assertTrue(bean.destroyCalled);
}
 
Example #5
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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");
	assertTrue(bean.initCalled);
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
}
 
Example #6
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationWithManualConfiguration() throws Exception {
	InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
	bpp.setInitAnnotationType(PostConstruct.class);
	bpp.setDestroyAnnotationType(PreDestroy.class);
	InitDestroyAnnotationBeanPostProcessor bpp2 = (InitDestroyAnnotationBeanPostProcessor)
			SerializationTestUtils.serializeAndDeserialize(bpp);

	AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
	bpp2.postProcessBeforeDestruction(bean, "annotatedBean");
	assertTrue(bean.destroyCalled);
}
 
Example #7
Source File: ResetBeanPostProcessorCaches.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #8
Source File: ResetBeanPostProcessorCaches.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
public static void resetInitDestroyAnnotationBeanPostProcessorCache(InitDestroyAnnotationBeanPostProcessor bpp) {
    try {
        Field field = InitDestroyAnnotationBeanPostProcessor.class.getDeclaredField("lifecycleMetadataCache");
        field.setAccessible(true);
        Map lifecycleMetadataCache = (Map) field.get(bpp);
        lifecycleMetadataCache.clear();
        LOGGER.trace("Cache cleared: InitDestroyAnnotationBeanPostProcessor.lifecycleMetadataCache");
    } catch (Exception e) {
        throw new IllegalStateException("Unable to clear InitDestroyAnnotationBeanPostProcessor.lifecycleMetadataCache", e);
    }
}