Spring容器中的Bean幾種初始化方法和銷燬方法的先後順序
阿新 • • 發佈:2019-01-07
Spring 容器中的 Bean 是有生命週期的,spring 允許 Bean 在初始化完成後以及銷燬前執行特定的操作。下面是常用的三種指定特定操作的方法:
- 通過實現InitializingBean/DisposableBean 介面來定製初始化之後/銷燬之前的操作方法;
- 通過<bean> 元素的 init-method/destroy-method屬性指定初始化之後 /銷燬之前呼叫的操作方法;
- 在指定方法上加上@PostConstruct或@PreDestroy註解來制定該方法是在初始化之後還是銷燬之前呼叫。
這幾種配置方式,執行順序是怎樣的呢?我們通過例子來研究下:
Java類:
-
import
- import javax.annotation.PreDestroy;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
publicclass InitAndDestroySeqBean
- public InitAndDestroySeqBean(){
- System.out.println("執行InitAndDestroySeqBean: 構造方法");
- }
- @PostConstruct
- publicvoid postConstruct() {
- System.out.println("執行InitAndDestroySeqBean: postConstruct");
- }
-
@Override
- publicvoid afterPropertiesSet() throws Exception {
- System.out.println("執行InitAndDestroySeqBean: afterPropertiesSet");
- }
- publicvoid initMethod() {
- System.out.println("執行InitAndDestroySeqBean: init-method");
- }
- @PreDestroy
- publicvoid preDestroy() {
- System.out.println("執行InitAndDestroySeqBean: preDestroy");
- }
- @Override
- publicvoid destroy() throws Exception {
- System.out.println("執行InitAndDestroySeqBean: destroy");
- }
- publicvoid destroyMethod() {
- System.out.println("執行InitAndDestroySeqBean: destroy-method");
- }
- publicstaticvoid main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
- context.close();
- }
- }
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- <beanid="initAndDestroySeqBean"class="com.chj.spring.InitAndDestroySeqBean"init-method="initMethod"destroy-method="destroyMethod"/>
- </beans>
- 2013-03-03 17:11:19,098 DEBUG support.DefaultListableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'
- 執行InitAndDestroySeqBean: 構造方法
- 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
- 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
- 執行InitAndDestroySeqBean: postConstruct
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'
- 執行InitAndDestroySeqBean: afterPropertiesSet
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking init method 'initMethod' on bean with name 'initAndDestroySeqBean'
- 執行InitAndDestroySeqBean: init-method
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'
- 2013-03-03 17:11:19,129 INFO support.ClassPathXmlApplicationContext - Closing org[email protected]56a499: display name [org[email protected]56a499]; startup date [Sun Mar 03 17:11:17 CST 2013]; root of context hierarchy
- 2013-03-03 17:11:19,129 INFO support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]1292d26: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,initAndDestroySeqBean]; root of factory hierarchy
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
- 執行InitAndDestroySeqBean: preDestroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'
- 執行InitAndDestroySeqBean: destroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'
- 執行InitAndDestroySeqBean: destroy-method
Bean在例項化的過程中:Constructor > @PostConstruct >InitializingBean > init-method
Bean在銷燬的過程中:@PreDestroy > DisposableBean > destroy-method