前言
Spring的ioc容器功能非常強大,負責Spring的Bean的創建和管理等功能。而Spring 的bean是整個Spring應用中很重要的一部分,了解Spring Bean的生命周期對我們了解整個spring框架會有很大的幫助。
BeanFactory和ApplicationContext是Spring兩種很重要的容器,前者提供了最基本的依賴注入的支持,而后者在繼承前者的基礎進行了功能的拓展,例如增加了事件傳播,資源訪問和國際化的消息訪問等功能。本文主要介紹了ApplicationContext和BeanFactory兩種容器的Bean的生命周期。
首先看下生命周期圖:
再談生命周期之前有一點需要先明確:
Spring 只幫我們管理單例模式 Bean 的 完整 生命周期,對于 prototype 的 bean ,Spring 在創建好交給使用者之后則不會再管理后續的生命周期。
注解方式
在 bean 初始化時會經歷幾個階段,首先可以使用注解 @PostConstruct , @PreDestroy 來在 bean 的創建和銷毀階段進行調用:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class AnnotationBean { private final static Logger LOGGER = LoggerFactory.getLogger(AnnotationBean. class ); @PostConstruct public void start(){ LOGGER.info( "AnnotationBean start" ); } @PreDestroy public void destroy(){ LOGGER.info( "AnnotationBean destroy" ); } } |
InitializingBean, DisposableBean 接口
還可以實現 InitializingBean,DisposableBean 這兩個接口,也是在初始化以及銷毀階段調用:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Service public class SpringLifeCycleService implements InitializingBean,DisposableBean{ private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleService. class ); @Override public void afterPropertiesSet() throws Exception { LOGGER.info( "SpringLifeCycleService start" ); } @Override public void destroy() throws Exception { LOGGER.info( "SpringLifeCycleService destroy" ); } } |
自定義初始化和銷毀方法
也可以自定義方法用于在初始化、銷毀階段調用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Configuration public class LifeCycleConfig { @Bean (initMethod = "start" , destroyMethod = "destroy" ) public SpringLifeCycle create(){ SpringLifeCycle springLifeCycle = new SpringLifeCycle() ; return springLifeCycle ; } } public class SpringLifeCycle{ private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycle. class ); public void start(){ LOGGER.info( "SpringLifeCycle start" ); } public void destroy(){ LOGGER.info( "SpringLifeCycle destroy" ); } } |
以上是在 SpringBoot 中可以這樣配置,如果是原始的基于 XML 也是可以使用:
1
2
|
<bean class = "com.crossoverjie.spring.SpringLifeCycle" init-method= "start" destroy-method= "destroy" > </bean> |
來達到同樣的效果。
實現 *Aware 接口
*Aware 接口可以用于在初始化 bean 時獲得 Spring 中的一些對象,如獲取 Spring 上下文 等。
1
2
3
4
5
6
7
8
9
10
|
@Component public class SpringLifeCycleAware implements ApplicationContextAware { private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleAware. class ); private ApplicationContext applicationContext ; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this .applicationContext = applicationContext ; LOGGER.info( "SpringLifeCycleAware start" ); } } |
這樣在 springLifeCycleAware 這個 bean 初始化會就會調用 setApplicationContext 方法,并可以獲得 applicationContext 對象。
BeanPostProcessor 增強處理器
實現 BeanPostProcessor 接口,Spring 中所有 bean 在做初始化時都會調用該接口中的兩個方法,可以用于對一些特殊的 bean 進行處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@Component public class SpringLifeCycleProcessor implements BeanPostProcessor { private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleProcessor. class ); /** * 預初始化 初始化之前調用 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if ( "annotationBean" .equals(beanName)){ LOGGER.info( "SpringLifeCycleProcessor start beanName={}" ,beanName); } return bean; } /** * 后初始化 bean 初始化完成調用 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if ( "annotationBean" .equals(beanName)){ LOGGER.info( "SpringLifeCycleProcessor end beanName={}" ,beanName); } return bean; } } |
執行之后觀察結果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
018 - 03 - 21 00 : 40 : 24.856 [restartedMain] INFO c.c.s.p.SpringLifeCycleProcessor - SpringLifeCycleProcessor start beanName=annotationBean 2018 - 03 - 21 00 : 40 : 24.860 [restartedMain] INFO c.c.spring.annotation.AnnotationBean - AnnotationBean start 2018 - 03 - 21 00 : 40 : 24.861 [restartedMain] INFO c.c.s.p.SpringLifeCycleProcessor - SpringLifeCycleProcessor end beanName=annotationBean 2018 - 03 - 21 00 : 40 : 24.864 [restartedMain] INFO c.c.s.aware.SpringLifeCycleAware - SpringLifeCycleAware start 2018 - 03 - 21 00 : 40 : 24.867 [restartedMain] INFO c.c.s.service.SpringLifeCycleService - SpringLifeCycleService start 2018 - 03 - 21 00 : 40 : 24.887 [restartedMain] INFO c.c.spring.SpringLifeCycle - SpringLifeCycle start 2018 - 03 - 21 00 : 40 : 25.062 [restartedMain] INFO o.s.b.d.a.OptionalLiveReloadServer - LiveReload server is running on port 35729 2018 - 03 - 21 00 : 40 : 25.122 [restartedMain] INFO o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup 2018 - 03 - 21 00 : 40 : 25.140 [restartedMain] INFO com.crossoverjie.Application - Started Application in 2.309 seconds (JVM running for 3.681 ) 2018 - 03 - 21 00 : 40 : 25.143 [restartedMain] INFO com.crossoverjie.Application - start ok! 2018 - 03 - 21 00 : 40 : 25.153 [Thread- 8 ] INFO o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext @3913adad : startup date [Wed Mar 21 00 : 40 : 23 CST 2018 ]; root of context hierarchy 2018 - 03 - 21 00 : 40 : 25.155 [Thread- 8 ] INFO o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown 2018 - 03 - 21 00 : 40 : 25.156 [Thread- 8 ] INFO c.c.spring.SpringLifeCycle - SpringLifeCycle destroy 2018 - 03 - 21 00 : 40 : 25.156 [Thread- 8 ] INFO c.c.s.service.SpringLifeCycleService - SpringLifeCycleService destroy 2018 - 03 - 21 00 : 40 : 25.156 [Thread- 8 ] INFO c.c.spring.annotation.AnnotationBean - AnnotationBean destroy |
直到 Spring 上下文銷毀時則會調用自定義的銷毀方法以及實現了 DisposableBean 的 destroy() 方法。
總結
以上所述是小編給大家介紹的Spring Bean 生命周期,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://juejin.im/post/5ab1bf19f265da23771947f1