@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); }
@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); }
@Override public void postProcessBeanFactory(BundleContext context, ConfigurableListableBeanFactory factory) throws BeansException, InvalidSyntaxException, BundleException { // We need to set the beanfactory by hand. This MAY be a bug in spring-dm but I'm not sure yet LogEnabledBeanPostProcessor loggingProcessor = new LogEnabledBeanPostProcessor(); loggingProcessor.setBeanFactory(factory); loggingProcessor.setLogProvider(logProvider); loggingProcessor.setOrder(0); factory.addBeanPostProcessor(loggingProcessor); OSGIResourceAnnotationBeanPostProcessor resourceProcessor = new OSGIResourceAnnotationBeanPostProcessor(); resourceProcessor.setBeanClassLoader(factory.getBeanClassLoader()); resourceProcessor.setBeanFactory(factory); resourceProcessor.setBundleContext(context); resourceProcessor.setTimeout(60 * 1000); factory.addBeanPostProcessor(resourceProcessor); OSGIPersistenceUnitAnnotationBeanPostProcessor persistenceProcessor = new OSGIPersistenceUnitAnnotationBeanPostProcessor(); persistenceProcessor.setBeanClassLoader(factory.getBeanClassLoader()); persistenceProcessor.setBeanFactory(factory); persistenceProcessor.setBundleContext(context); persistenceProcessor.setTimeout(60 * 1000); factory.addBeanPostProcessor(persistenceProcessor); ConfigurableBeanPostProcessor configurationProcessor = new ConfigurableBeanPostProcessor(); configurationProcessor.setBeanFactory(factory); configurationProcessor.setConfigurationProvider(confProvider); configurationProcessor.setOrder(2); factory.addBeanPostProcessor(configurationProcessor); InitDestroyAnnotationBeanPostProcessor annotationProcessor = new InitDestroyAnnotationBeanPostProcessor(); annotationProcessor.setInitAnnotationType(PostConstruct.class); annotationProcessor.setDestroyAnnotationType(PreDestroy.class); factory.addBeanPostProcessor(annotationProcessor); }
/** * 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); } } }
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); } }