前言
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 的創建和銷毀階段進行調用:
@Componentpublic 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 這兩個接口,也是在初始化以及銷毀階段調用:
@Servicepublic 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"); }}自定義初始化和銷毀方法
也可以自定義方法用於在初始化、銷毀階段調用:
@Configurationpublic 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 也是可以使用:
<bean init-method="start" destroy-method="destroy"></bean>
來達到同樣的效果。
實現*Aware 接口
*Aware 接口可以用於在初始化bean 時獲得Spring 中的一些對象,如獲取Spring 上下文等。
@Componentpublic 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 進行處理:
@Componentpublic 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; }}執行之後觀察結果:
018-03-21 00:40:24.856 [restartedMain] INFO ccspSpringLifeCycleProcessor - SpringLifeCycleProcessor start beanName=annotationBean2018-03-21 00:40:24.860 [restartedMain] INFO ccspring.annotation.AnnotationBean - AnnotationBean start2018-03-21 00:40:24.861 [restartedMain] INFO ccspSpringLifeCycleProcessor - SpringLifeCycleProcessor end beanName=annotationBean2018-03-21 00:40:24.864 [restartedMain] INFO ccsaware.SpringLifeCycleAware - SpringLifeCycleAware start2018-03-21 00:40:24.867 [restartedMain] INFO ccsservice.SpringLifeCycleService - SpringLifeCycleService start2018-03-21 00:40:24.887 [restartedMain] INFO ccspring.SpringLifeCycle - SpringLifeCycle start2018-03-21 00:40:25.062 [restartedMain] INFO osbdaOptionalLiveReloadServer - LiveReload server is running on port 357292018-03-21 00:40:25.122 [restartedMain] INFO osjeaAnnotationMBeanExporter - Registering beans for JMX exposure on startup2018-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 oscaAnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3913adad: startup date [Wed Mar 21 00:40:23 CST 2018]; root of context hierarchy2018-03-21 00:40:25.155 [Thread-8] INFO osjeaAnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown2018-03-21 00:40:25.156 [Thread-8] INFO ccspring.SpringLifeCycle - SpringLifeCycle destroy2018-03-21 00:40:25.156 [Thread-8] INFO ccsservice.SpringLifeCycleService - SpringLifeCycleService destroy2018-03-21 00:40:25.156 [Thread-8] INFO ccspring.annotation.AnnotationBean - AnnotationBean destroy
直到Spring 上下文銷毀時則會調用自定義的銷毀方法以及實現了DisposableBean 的destroy() 方法。
總結
以上所述是小編給大家介紹的Spring Bean 生命週期,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!