Spring中與bean有關(guān)的生命周期
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
66套java從入門到精通實(shí)戰(zhàn)課程分享
? 作者?|??eaglelihh
來(lái)源 |?cnblogs.com/eaglelihh/p/13383039.html
前言
記得以前的時(shí)候,每次提起Spring中的bean相關(guān)的生命周期時(shí),內(nèi)心都無(wú)比的恐懼,因?yàn)楹孟裼泻芏啵约河掷聿磺宄菏裁碽eanFactory啊,aware接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。
今天終于理清這些關(guān)系了,并且又新增了對(duì)postConstruct和lifecycle的理解。
執(zhí)行順序
- 首先是 BeanFactoryPostProcessor,它是針對(duì)所有bean的definition的,只執(zhí)行一次
下面是針對(duì)每個(gè)bean的初始
- 實(shí)現(xiàn)了一系列aware接口的,比如BeanNameAware,ApplicationContextAware,調(diào)用其set方法
- 執(zhí)行BeanPostProcessor的postProcessBeforeInitialization方法
- 帶有@PostConstruct注解的方法
- 實(shí)現(xiàn)InitializingBean接口的afterPropertiesSet方法
- 指定的initMethod方法
- 執(zhí)行BeanPostProcessor的postProcessAfterInitialization方法
- 實(shí)現(xiàn)了SmartLifecycle接口的start方法(實(shí)現(xiàn)Lifecycle接口的不會(huì)自動(dòng)調(diào)用,需要顯式的調(diào)用start方法)
下面是針對(duì)每個(gè)bean的銷毀
- 實(shí)現(xiàn)了SmartLifecycle接口的stop方法(實(shí)現(xiàn)Lifecycle接口的不會(huì)自動(dòng)調(diào)用,需要顯式的調(diào)用stop方法)
- 帶有@PreDestroy注解的方法
- 實(shí)現(xiàn)DisposableBean接口的destroy方法
- 指定的destroyMethod方法
目前就想到這么多了,其他的麻煩在評(píng)論區(qū)留言呀~代碼實(shí)例
bean實(shí)體類
/**
?* @date: 2020-07-22
?*
?* 一個(gè)簡(jiǎn)單的枚舉類
?*/
public?enum?BeanType {
????NORMAL, LIFECYCLE, SMART_LIFECYCLE;
}
import?javax.annotation.PostConstruct;
import?javax.annotation.PreDestroy;
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.beans.BeansException;
import?org.springframework.beans.factory.BeanNameAware;
import?org.springframework.beans.factory.DisposableBean;
import?org.springframework.beans.factory.InitializingBean;
import?org.springframework.context.ApplicationContext;
import?org.springframework.context.ApplicationContextAware;
/**
?* @author: lihui
?* @date: 2020-07-22
?* 一個(gè)簡(jiǎn)單的bean
?*/
@Slf4j
public?class?NormalBean?implements?BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean?{
????private?BeanType beanType;
????public?NormalBean()?{
????????this(BeanType.NORMAL);
????}
????public?NormalBean(BeanType beanType)?{
????????this.beanType = beanType;
????}
????@PostConstruct
????public?void?postConstruct()?{
????????log.info("{}, postConstruct", beanType);
????}
????@Override
????public?void?afterPropertiesSet()?throws?Exception {
????????log.info("{}, afterPropertiesSet", beanType);
????}
????public?void?initMethod()?{
????????log.info("{}, initMethod", beanType);
????}
????@PreDestroy
????public?void?preDestroy()?{
????????log.info("{}, preDestroy", beanType);
????}
????@Override
????public?void?destroy()?throws?Exception {
????????log.info("{}, destroy", beanType);
????}
????public?void?destroyMethod()?{
????????log.info("{}, destroyMethod", beanType);
????}
????@Override
????public?void?setApplicationContext(ApplicationContext applicationContext)?throws?BeansException {
????????log.info("setApplicationContext, applicationContext : {}", applicationContext);
????}
????@Override
????public?void?setBeanName(String name)?{
????????log.info("setBeanName, bean name : {}", name);
????}
}
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.context.Lifecycle;
/**
?* @author: lihui
?* @date: 2020-07-22
?* 實(shí)現(xiàn)了Lifecycle的一個(gè)bean
?*/
@Slf4j
public?class?LifecycleBean?extends?NormalBean?implements?Lifecycle?{
????private?volatile?boolean?running = false;
????public?LifecycleBean()?{
????????super(BeanType.LIFECYCLE);
????}
????@Override
????public?void?start()?{
????????log.info("start");
????????running = true;
????}
????@Override
????public?void?stop()?{
????????log.info("stop");
????????running = false;
????}
????@Override
????public?boolean?isRunning()?{
????????return?running;
????}
}
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.context.SmartLifecycle;
/**
?* @author: lihui
?* @date: 2020-07-22
?* 實(shí)現(xiàn)了SmartLifecycle的一個(gè)bean
?*/
@Slf4j
public?class?SmartLifecycleBean?extends?NormalBean?implements?SmartLifecycle?{
????private?volatile?boolean?running = false;
????public?SmartLifecycleBean()?{
????????super(BeanType.SMART_LIFECYCLE);
????}
????@Override
????public?void?start()?{
????????log.info("start");
????????running = true;
????}
????@Override
????public?void?stop()?{
????????log.info("stop");
????????running = false;
????}
????@Override
????public?boolean?isRunning()?{
????????return?running;
????}
}配置類
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.beans.BeansException;
import?org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import?org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
/**
?* @author: lihui
?* @date: 2020-07-25
?*/
@Slf4j
public?class?MyBeanFactoryPostProcessor?implements?BeanFactoryPostProcessor?{
????@Override
????public?void?postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)?throws?BeansException {
????????log.info("postProcessBeanFactory, beanFactory:{}", beanFactory);
????}
}
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.beans.BeansException;
import?org.springframework.beans.factory.config.BeanPostProcessor;
/**
?* @author: lihui
?* @date: 2020-07-25
?*/
@Slf4j
public?class?MyBeanPostProcessor?implements?BeanPostProcessor?{
????@Override
????public?Object postProcessBeforeInitialization(Object bean, String beanName)?throws?BeansException {
????????log.info("postProcessBeforeInitialization, bean:{}", beanName);
????????return?bean;
????}
????@Override
????public?Object postProcessAfterInitialization(Object bean, String beanName)?throws?BeansException {
????????log.info("postProcessAfterInitialization, bean:{}", beanName);
????????return?bean;
????}
}
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.context.SmartLifecycle;
import?org.springframework.context.annotation.Bean;
import?org.springframework.context.annotation.Configuration;
/**
?* @author: lihui
?* @date: 2020-07-22
?*/
@Configuration
@Slf4j
public?class?Config?{
????@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
????public?NormalBean normalBean()?{
????????return?new?NormalBean();
????}
????@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
????public?LifecycleBean lifecycleBean()?{
????????return?new?LifecycleBean();
????}
????@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
????public?SmartLifecycle smartLifecycle()?{
????????return?new?SmartLifecycleBean();
????}
????@Bean
????public?static?MyBeanFactoryPostProcessor myBeanFactoryPostProcessor()?{
????????return?new?MyBeanFactoryPostProcessor();
????}
????@Bean
????public?static?MyBeanPostProcessor myBeanPostProcessor()?{
????????return?new?MyBeanPostProcessor();
????}
}Main類
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.context.ConfigurableApplicationContext;
import?org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
?* @author: lihui
?* @date: 2020-07-22
?*/
@Slf4j
public?class?Main?{
????public?static?void?main(String[] args)?throws InterruptedException {
????????ConfigurableApplicationContext ctx = new?AnnotationConfigApplicationContext(Config.class);
????????ctx.registerShutdownHook();
????????Thread.sleep(5000);
????????log.info("line ----------------------------- line");
????????ctx.start();
????????ctx.stop();
????????log.info("line ----------------------------- line");
????}
}結(jié)果說(shuō)明
結(jié)果正如前面所說(shuō)的執(zhí)行順序一致,主要注意的就是Lifecycle接口和SmartLifecycle接口,只有實(shí)現(xiàn)了SmartLifecycle接口的bean在初始化時(shí)才會(huì)被自動(dòng)調(diào)用,而實(shí)現(xiàn)了Lifecycle接口的除非顯式調(diào)用start和stop方法才會(huì)被調(diào)用。
502???2020-07-26?23:43:41.808?[main]?INFO??c.e.d.s.a.i.l.MyBeanFactoryPostProcessor?-?postProcessBeanFactory, beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@55ca8de8: defining?beans?[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,config,normalBean,lifecycleBean,smartLifecycle,myBeanFactoryPostProcessor,myBeanPostProcessor]; root?of?factory?hierarchy
520???2020-07-26?23:43:41.826?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessBeforeInitialization, bean:config
520???2020-07-26?23:43:41.826?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessAfterInitialization, bean:config
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setBeanName, bean?name?: normalBean
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setApplicationContext, applicationContext?: org.springframework.context.annotation.AnnotationConfigApplicationContext@7cd62f43, started?on?Sun?Jul?26?23:43:41?CST?2020
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessBeforeInitialization, bean:normalBean
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, postConstruct
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, afterPropertiesSet
536???2020-07-26?23:43:41.842?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, initMethod
537???2020-07-26?23:43:41.843?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessAfterInitialization, bean:normalBean
538???2020-07-26?23:43:41.844?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setBeanName, bean?name?: lifecycleBean
538???2020-07-26?23:43:41.844?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setApplicationContext, applicationContext?: org.springframework.context.annotation.AnnotationConfigApplicationContext@7cd62f43, started?on?Sun?Jul?26?23:43:41?CST?2020
538???2020-07-26?23:43:41.844?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessBeforeInitialization, bean:lifecycleBean
538???2020-07-26?23:43:41.844?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, postConstruct
540???2020-07-26?23:43:41.846?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, afterPropertiesSet
540???2020-07-26?23:43:41.846?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, initMethod
540???2020-07-26?23:43:41.846?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessAfterInitialization, bean:lifecycleBean
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setBeanName, bean?name?: smartLifecycle
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?setApplicationContext, applicationContext?: org.springframework.context.annotation.AnnotationConfigApplicationContext@7cd62f43, started?on?Sun?Jul?26?23:43:41?CST?2020
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessBeforeInitialization, bean:smartLifecycle
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, postConstruct
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, afterPropertiesSet
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, initMethod
541???2020-07-26?23:43:41.847?[main]?INFO??c.e.d.s.a.i.l.MyBeanPostProcessor?-?postProcessAfterInitialization, bean:smartLifecycle
550???2020-07-26?23:43:41.856?[main]?INFO??c.e.d.s.a.i.l.SmartLifecycleBean?-?start
5562??2020-07-26?23:43:46.868?[main]?INFO??c.e.d.s.a.ioc.lifecycle.Main?-?line?-----------------------------?line
5563??2020-07-26?23:43:46.869?[main]?INFO??c.e.d.s.a.i.lifecycle.LifecycleBean?-?start
5566??2020-07-26?23:43:46.872?[main]?INFO??c.e.d.s.a.i.l.SmartLifecycleBean?-?stop
5566??2020-07-26?23:43:46.872?[main]?INFO??c.e.d.s.a.i.lifecycle.LifecycleBean?-?stop
5566??2020-07-26?23:43:46.872?[main]?INFO??c.e.d.s.a.ioc.lifecycle.Main?-?line?-----------------------------?line
5576??2020-07-26?23:43:46.882?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, preDestroy
5576??2020-07-26?23:43:46.882?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, destroy
5576??2020-07-26?23:43:46.882?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?SMART_LIFECYCLE, destroyMethod
5576??2020-07-26?23:43:46.882?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, preDestroy
5576??2020-07-26?23:43:46.882?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, destroy
5577??2020-07-26?23:43:46.883?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?LIFECYCLE, destroyMethod
5577??2020-07-26?23:43:46.883?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, preDestroy
5577??2020-07-26?23:43:46.883?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, destroy
5577??2020-07-26?23:43:46.883?[SpringContextShutdownHook]?INFO??c.e.d.s.a.ioc.lifecycle.NormalBean?-?NORMAL, destroyMethod

??? ?
感謝點(diǎn)贊支持下哈?
評(píng)論
圖片
表情
