/** * Destroy the inner bean factory (used for scripts) on shutdown. */ @Override public void destroy() { this.scriptBeanFactory.destroySingletons(); }
/** * We must simulate container shutdown, which should clear threads. */ @After public void tearDown() { // Will call pool.close() this.beanFactory.destroySingletons(); }
@AfterClass public static void tearDownAfterClass() throws Exception { bf.destroySingletons(); }
/** * We must simulate container shutdown, which should clear threads. */ @After public void tearDown() { // Will call pool.close() this.beanFactory.destroySingletons(); }
@Test public void testCustomDestroyBean() throws Exception { beanFactory.getBean("custom-destroy"); beanFactory.destroySingletons(); assertNull(System.getProperty("security.destroy")); }
@Test public void testSpringDestroyBean() throws Exception { beanFactory.getBean("spring-destroy"); beanFactory.destroySingletons(); assertNull(System.getProperty("security.destroy")); }
@Test public void testInitializingDisposableInterfaces() { final Class<?> beanClass = CustomInitializingDisposableBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit", "customDestroy"); final CustomInitializingDisposableBean bean = (CustomInitializingDisposableBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("afterPropertiesSet", "customInit"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("destroy", "customDestroy"), bean.destroyMethods); }
@Test public void testJsr250AnnotationsWithShadowedMethods() { final Class<?> beanClass = CustomAnnotatedInitDestroyWithShadowedMethodsBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit", "customDestroy"); final CustomAnnotatedInitDestroyWithShadowedMethodsBean bean = (CustomAnnotatedInitDestroyWithShadowedMethodsBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("@PostConstruct.afterPropertiesSet", "customInit"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("@PreDestroy.destroy", "customDestroy"), bean.destroyMethods); }
@Test public void testAllLifecycleMechanismsAtOnce() { final Class<?> beanClass = AllInOneBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "afterPropertiesSet", "destroy"); final AllInOneBean bean = (AllInOneBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("afterPropertiesSet"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("destroy"), bean.destroyMethods); }
@Test public void testInitializingDisposableInterfacesWithShadowedMethods() { final Class<?> beanClass = InitializingDisposableWithShadowedMethodsBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "afterPropertiesSet", "destroy"); final InitializingDisposableWithShadowedMethodsBean bean = (InitializingDisposableWithShadowedMethodsBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("InitializingBean.afterPropertiesSet"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("DisposableBean.destroy"), bean.destroyMethods); }
@Test public void ignoreDefaultLifecycleMethods() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource( "ignoreDefaultLifecycleMethods.xml", getClass())); bf.preInstantiateSingletons(); bf.destroySingletons(); }
@Test public void testInitDestroyMethods() { final Class<?> beanClass = InitDestroyBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "afterPropertiesSet", "destroy"); final InitDestroyBean bean = (InitDestroyBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("afterPropertiesSet"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("destroy"), bean.destroyMethods); }
@Test public void testJsr250Annotations() { final Class<?> beanClass = CustomAnnotatedInitDestroyBean.class; final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit", "customDestroy"); final CustomAnnotatedInitDestroyBean bean = (CustomAnnotatedInitDestroyBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN); assertMethodOrdering(beanClass, "init-methods", Arrays.asList("postConstruct", "afterPropertiesSet", "customInit"), bean.initMethods); beanFactory.destroySingletons(); assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("preDestroy", "destroy", "customDestroy"), bean.destroyMethods); }
@Test public void testPostProcessorWithNullBean() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor()); RootBeanDefinition rbd = new RootBeanDefinition(NullFactory.class); rbd.setFactoryMethodName("create"); bf.registerBeanDefinition("bean", rbd); assertEquals("null", bf.getBean("bean").toString()); bf.destroySingletons(); }
@Test public void testPostConstructAndPreDestroy() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor()); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class)); AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); bf.destroySingletons(); assertTrue(bean.destroyCalled); }
@Test public void testPostConstructAndPreDestroyWithPostProcessor() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.addBeanPostProcessor(new InitDestroyBeanPostProcessor()); bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor()); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class)); AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); bf.destroySingletons(); assertTrue(bean.destroyCalled); }
@Test public void childWithLifecycleMethodsDisabled() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithLifecycleMethodsDisabled"); assertFalse("Bean init method called incorrectly", bean.isInitCalled()); assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); this.beanFactory.destroySingletons(); assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled()); assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); }
@Test public void overrideDefaultLifecycleMethods() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("overrideLifecycleMethods"); assertFalse("Default init method called incorrectly", bean.isInitCalled()); assertTrue("Custom init method not called", bean.isCustomInitCalled()); this.beanFactory.destroySingletons(); assertFalse("Default destroy method called incorrectly", bean.isDestroyCalled()); assertTrue("Custom destroy method not called", bean.isCustomDestroyCalled()); }
@Test public void lifecycleMethodsDisabled() throws Exception { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleMethodsDisabled"); assertFalse("Bean init method called incorrectly", bean.isInitCalled()); assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); this.beanFactory.destroySingletons(); assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled()); assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); }
@Test public void lifecycleMethodsInvoked() { LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleAware"); assertTrue("Bean not initialized", bean.isInitCalled()); assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled()); assertFalse("Bean destroyed too early", bean.isDestroyCalled()); this.beanFactory.destroySingletons(); assertTrue("Bean not destroyed", bean.isDestroyCalled()); assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled()); }