Spring与bean有关的生命周期示例详解
前言
记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚:什么beanFactory啊,aware接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。
今天终于理清这些关系了,并且又新增了对postConstruct和lifecycle的理解。
执行顺序
- 首先是 BeanFactoryPostProcessor,它是针对所有bean的definition的,只执行一次
下面是针对每个bean的初始
- - 实现了一系列aware接口的,比如BeanNameAware,ApplicationContextAware,调用其set方法
- - 执行BeanPostProcessor的postProcessBeforeInitialization方法
- - 带有@PostConstruct注解的方法
- - 实现InitializingBean接口的afterPropertiesSet方法
- - 指定的initMethod方法
- - 执行BeanPostProcessor的postProcessAfterInitialization方法
- - 实现了SmartLifecycle接口的start方法(实现Lifecycle接口的不会自动调用,需要显式的调用start方法)
下面是针对每个bean的销毁
- - 实现了SmartLifecycle接口的stop方法(实现Lifecycle接口的不会自动调用,需要显式的调用stop方法)
- - 带有@PreDestroy注解的方法
- - 实现DisposableBean接口的destroy方法
- - 指定的destroyMethod方法
目前就想到这么多了,其他的麻烦在评论区留言呀~
代码实例
bean实体类
/** * @date: 2020-07-22 * * 一个简单的枚举类 */ 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 * 一个简单的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 * 实现了Lifecycle的一个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 * 实现了SmartLifecycle的一个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"); } }
结果说明
结果正如前面所说的执行顺序一致,主要注意的就是Lifecycle接口和SmartLifecycle接口,只有实现了SmartLifecycle接口的bean在初始化时才会被自动调用,而实现了Lifecycle接口的除非显式调用start和stop方法才会被调用。
总结
到此这篇关于Spring与bean有关的生命周期的文章就介绍到这了,更多相关Spring与bean生命周期内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
相关文章
- 这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
Spring Cloud 中@FeignClient注解中的contextId属性详解
这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-25Springboot如何实现Web系统License授权认证
这篇文章主要介绍了Springboot如何实现Web系统License授权认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-28如何在Spring WebFlux的任何地方获取Request对象
这篇文章主要介绍了如何在Spring WebFlux的任何地方获取Request对象,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2021-01-26Vue生命周期activated之返回上一页不重新请求数据操作
这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26- 这篇文章主要介绍了详解SpringCloudGateway内存泄漏问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-16
- @Autowired 注解的主要功能就是完成自动注入,使用也非常简单,但这篇文章主要给大家介绍了关于Spring为什么不推荐使用@Autowired注解的相关资料,需要的朋友可以参考下...2021-11-03
Springboot如何使用mybatis实现拦截SQL分页
这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19- 这篇文章主要介绍了SpringMVC文件上传原理及实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-15
Spring Data JPA 关键字Exists的用法说明
这篇文章主要介绍了Spring Data JPA 关键字Exists的用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-10tomcat启动完成执行 某个方法 定时任务(Spring)操作
这篇文章主要介绍了tomcat启动完成执行 某个方法 定时任务(Spring)操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25- 这篇文章主要介绍了Springboot实现多线程注入bean的工具类操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
使用Maven 搭建 Spring MVC 本地部署Tomcat的详细教程
这篇文章主要介绍了使用Maven 搭建 Spring MVC 本地部署Tomcat,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-08-16- 这篇文章主要介绍了Spring Cloud负载均衡及远程调用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2021-09-18
- 这篇文章主要介绍了SpringMvc自动装箱及GET请求参数原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-19
- 这篇文章主要介绍了SpringMvc获取请求头请求体消息过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-17
- 这篇文章主要介绍了Springboot使用thymeleaf动态模板实现刷新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-08-31
Idea打包springboot项目没有.original文件解决方案
这篇文章主要介绍了Idea打包springboot项目没有.original文件解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-26- 这篇文章主要介绍了spring boot 使用utf8mb4的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-20
Springmvc ResponseBody响应json数据实现过程
这篇文章主要介绍了Springmvc ResponseBody响应json数据实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-10-26