欢迎关注方志朋的博客,回复”666“获面试宝典

本文基于SpringBoot 2.5.0-M2讲解Spring中Lifecycle和SmartLifecycle的作用和区别,以及如何控制SmartLifecycle的优先级。

并讲解SpringBoot中如何通过SmartLifecycle来启动/停止web容器.

SmartLifecycle & Lifecycle作用和区别

SmartLifecycle和Lifecycle作用

都是让开发者可以在所有的bean都创建完成(getBean) 之后执行自己的初始化工作,或者在退出时执行资源销毁工作。

SmartLifecycle和Lifecycle区别

  • SmartLifecycle接口继承Lifecycle接口,同时继承了org.springframework.context.Phased接口用于控制多个SmartLifecycle实现之间的优先级。

  • 在SpringBoot应用中,或在Spring应用中没有调用AbstractApplicationContext#start方法,如果一个Bean只是实现了Lifecycle接口的情况下:

    • 不会执行Lifecycle接口中的启动方法,包括Lifecycle#isRunning方法也不会被执行。

    • 但是在应用 退出时 会执行Lifecycle#isRunning方法判断该Lifecycle是否已经启动,如果返回true则调用Lifecycle#stop()停止方法。

  • 如果一个Bean实现了SmartLifecycle接口,则会执行启动方法。先会被根据Phased接口优先级分组,封装在LifecycleGroup,然后循环调用LifecycleGroup#start()方法,SmartLifecycle#isRunning判断是否已经执行,返回false表示还未执行,则调用SmartLifecycle#start()执行。Phased返回值越小,优先级越高。

  • SmartLifecycle中还有个isAutoStartup方法,如果返回false,在启动时也不会执行start方法,默认返回true

源码分析

SmartLifecycle和Lifecycle都是在org.springframework.context.support.DefaultLifecycleProcessor中被调用,DefaultLifecycleProcessor#onRefresh方法在执行AbstractApplicationContext#finishRefresh时会被调用,调用栈如下:

startBeans:142, DefaultLifecycleProcessor (org.springframework.context.support)
onRefresh:123, DefaultLifecycleProcessor (org.springframework.context.support)
finishRefresh:934, AbstractApplicationContext (org.springframework.context.support)
refresh:585, AbstractApplicationContext (org.springframework.context.support)
refresh:144, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:755, SpringApplication (org.springframework.boot)
refreshContext:426, SpringApplication (org.springframework.boot)
run:326, SpringApplication (org.springframework.boot)
run:1299, SpringApplication (org.springframework.boot)
run:1288, SpringApplication (org.springframework.boot)
main:31, DemoApplication (com.example.demo)

DefaultLifecycleProcessor#onRefresh源码:

@Override
public void onRefresh() {startBeans(true);  //autoStartupOnly = truethis.running = true;
}

DefaultLifecycleProcessor#startBeans源码如下:

autoStartupOnly 在onRefresh时传入的是true,表示只执行可以自动启动的bean,即为:SmartLifecycle的实现类,并且SmartLifecycle#isAutoStartup返回值必须为true。

private void startBeans(boolean autoStartupOnly) {Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();Map<Integer, LifecycleGroup> phases = new TreeMap<>();lifecycleBeans.forEach((beanName, bean) -> {if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {int phase = getPhase(bean);phases.computeIfAbsent(phase, p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)).add(beanName, bean);}});if (!phases.isEmpty()) {phases.values().forEach(LifecycleGroup::start);}
}

而Spring AbstractApplicationContext#doClose退出时,无论是SmartLifecycle或Lifecycle都会执行isRunning方法,判断是否已经启动,返回true表示已经启动,则执行SmartLifecycle或Lifecycle的stop方法。源码见:org.springframework.context.support.DefaultLifecycleProcessor#doStop方法。

而执行AbstractApplicationContext#doClose一般是应用进程退出,通过jvm注册的钩子方法,或者应用程序编码调用。

AbstractApplicationContext#registerShutdownHook源码

@Override
public void registerShutdownHook() {if (this.shutdownHook == null) {// No shutdown hook registered yet.this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {@Overridepublic void run() {synchronized (startupShutdownMonitor) {doClose();}}};Runtime.getRuntime().addShutdownHook(this.shutdownHook);}
}

自定义LifecycleProcessor处理Lifecycle

在源码分析中提到了DefaultLifecycleProcessor,其实现了LifecycleProcessor接口。然而我们自己也可以实现该接口,替换默认的DefaultLifecycleProcessor。SpringBoot中则是自己配置了DefaultLifecycleProcessor,我们可以按照同样的方式,覆盖默认的实现。例如可以让Lifecycle中的start()方法在onRefresh()时也能被执行。

org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration源码:

/*** {@link EnableAutoConfiguration Auto-configuration} relating to the application* context's lifecycle.** @author Andy Wilkinson* @since 2.3.0*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(LifecycleProperties.class)
public class LifecycleAutoConfiguration {@Bean(name = AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME)@ConditionalOnMissingBean(name = AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME,search = SearchStrategy.CURRENT)public DefaultLifecycleProcessor defaultLifecycleProcessor(LifecycleProperties properties) {DefaultLifecycleProcessor lifecycleProcessor = new DefaultLifecycleProcessor();lifecycleProcessor.setTimeoutPerShutdownPhase(properties.getTimeoutPerShutdownPhase().toMillis());return lifecycleProcessor;}
}

SpringBoot中内嵌web容器启动时机

SpringBoo中就是通过实现SmartLifecycle来启动内嵌的web容器,实现类为WebServerStartStopLifecycle

ServletWebServerApplicationContext在onRefresh方法中调用createWebServer,createWebServer方法中创建org.springframework.boot.web.server.WebServer实例,该对象则包含了控制web容器(tomcat、jetty)的启动与停止方法。

@Override
protected void onRefresh() {super.onRefresh();try {createWebServer();}catch (Throwable ex) {throw new ApplicationContextException("Unable to start web server", ex);}
}

ServletWebServerApplicationContext#createWebServer源码:

private void createWebServer() {WebServer webServer = this.webServer;ServletContext servletContext = getServletContext();if (webServer == null && servletContext == null) {StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");ServletWebServerFactory factory = getWebServerFactory();createWebServer.tag("factory", factory.getClass().toString());this.webServer = factory.getWebServer(getSelfInitializer());createWebServer.end();getBeanFactory().registerSingleton("webServerGracefulShutdown",new WebServerGracefulShutdownLifecycle(this.webServer));getBeanFactory().registerSingleton("webServerStartStop",new WebServerStartStopLifecycle(this, this.webServer));}else if (servletContext != null) {try {getSelfInitializer().onStartup(servletContext);}catch (ServletException ex) {throw new ApplicationContextException("Cannot initialize servlet context", ex);}}initPropertySources();
}

createWebServer方法会将创建的webServer封装在WebServerStartStopLifecycle对象中,并注册到Spring容器中。

org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle源码如下:

class WebServerStartStopLifecycle implements SmartLifecycle {private final ServletWebServerApplicationContext applicationContext;private final WebServer webServer;private volatile boolean running;WebServerStartStopLifecycle(ServletWebServerApplicationContext applicationContext, WebServer webServer) {this.applicationContext = applicationContext;this.webServer = webServer;}@Overridepublic void start() {this.webServer.start();this.running = true;this.applicationContext.publishEvent(new ServletWebServerInitializedEvent(this.webServer, this.applicationContext));}@Overridepublic void stop() {  this.webServer.stop();   }@Overridepublic boolean isRunning() { return this.running;  }@Overridepublic int getPhase() {    return Integer.MAX_VALUE - 1;  }
}

WebServerStartStopLifecycle则实现了SmartLifecycle接口,当Spring回调到SmartLifecycle接口方法时则调用this.webServer.start();启动web容器,web容器启动完成之后会通过applicationContext发布ServletWebServerInitializedEvent事件,表示web容器启动成功,可以接收http请求。

和SmartInitializingSingleton区别

相同点:SmartInitializingSingleton和Lifecycle、SmartLifecycle都是在所有的单实例bean创建(getBean方法)之后执行。

不同点:

  • SmartInitializingSingleton优先于Lifecycle、SmartLifecycle执行。

  • SmartInitializingSingleton只有一个afterSingletonsInstantiated方法。而Lifecycle有start,stop,isRunning等方法。

  • 多个SmartInitializingSingleton实现之间无法排序控制执行的顺序,而SmartLifecycle实现了Phased接口,可以通过int getPhase()控制执行循序。

  • SmartInitializingSingleton之间可以通过@DependsOn来控制执行顺序,但这是由Spring中@DependsOn注解的作用及原理来实现的. 并不是对SmartInitializingSingleton做了排序。

来源:blog.csdn.net/u013202238/article/details/114489001

热门内容:
  • 使用雪花id或uuid作为Mysql主键,被老板怼了一顿!

  • 面试官:如果要存ip地址,用什么数据类型比较好

  • 干掉visio,这个画图神器真的绝了!!!

  • 真正的缓存之王,Google Guava 只是弟弟

  • 频频曝出程序员被抓,我们该如何避免面向监狱编程?

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

Spring中SmartLifecycle和Lifecycle的作用和区别相关推荐

  1. Spring中的拦截器的作用

    Spring中的拦截器的作用 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. 用户可以自己定义一些拦截器来实现特定的功能. 谈到拦截器 ...

  2. ANDROID 中UID与PID的作用与区别

    ANDROID 中UID与PID的作用与区别 PID:为Process Identifier, PID就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID.进程中止后PID被系统 ...

  3. linux中export和source的作用和区别

    linux中export和source的作用和区别 2013-11-12 12:36 1039人阅读 评论(0) 收藏 举报 分类: linux(82) shell与export命令 用户登录到Lin ...

  4. Spring中@Autowired、@Qualifier、@Resource的区别

    转自: Spring中@Autowired.@Qualifier.@Resource的区别_老周聊架构的博客-CSDN博客_qualifier和resource区别1.@Autowired@Autow ...

  5. java中factory_Java后台面试--Spring中FactoryBean与BeanFactory的使用及区别

    以前刚转Java的时候去面试被问到过Spring中FactoryBean与BeanFactory的使用及区别,由于之前没有重视这两个的区别,只是在配置文件里面加bean结点并通过注解的形式调用,所以被 ...

  6. Spring中的RowMapper接口的作用

    Spring中RowMapper接口的作用: 用来把数据库中的列字段和java bean中属性对应上,这样就可以赋值了 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类. 我 ...

  7. [读书笔记]Spring中BeanFactory和ApplicationContext的联系和区别

    写在前言 依赖反转是指依赖对象的获得被反转了.依赖控制反转的实现有很多方式,Spring中IOC容器是实现这个模式的载体,它可以在对象生成或初始化时直接将数据注入到对象中,也可以通过将对象引用注入到对 ...

  8. spring中基于Java容器配置注解的区别及使用场景

    转载自百家号作者:有趣的代码 @Component.@Service.@Controller.@Repository:@Configuration.@Bean区别,使用场景 从spring3.0开始, ...

  9. Python中3种引号的作用与区别

    简介: 本文主要介绍Python中,单引号,双引号,3个引号这3种引号的作用与区别. 1.简单介绍 单引号字符串:'python' 双引号字符串:"python" 三引号字符串:' ...

最新文章

  1. android读取assets中的html文件,android读取assets文件.htm
  2. Linux内核源码分析方法
  3. Linux采用存储管理方式,19.Linux采用( A )存储管理方式。
  4. 江南大学计算机科学esi排名,喜忧参半!2021年5月ESI世界大学排行榜,21个学科排名数据分析!...
  5. 蓝桥杯 ADV-234 算法提高 字符串跳步
  6. 置换群Polya定理(poj 2409: Let it Bead)
  7. UVa 674 Coin Change(完全背包)
  8. su灯光插件_su各种插件合集
  9. 阮一峰ES6之Generator函数理解
  10. 3d vision可以卸载吗_3D Vision是什么
  11. 学习webworker
  12. 转:sklearn 用户手册之1.12. 多类别与多标签算法
  13. 前端工程化-husky+commitizen+ 自定义cz-customizable适配器 git 提交代码规范化
  14. html中两列合并,表格怎么把两列内容合并到一起
  15. JS + shell 批量下载 喜马拉雅FM 的音频
  16. 25道经典大企Python面试题总结(附答案)
  17. Python连接postgresql数据库入门
  18. 「高效程序员的修炼」代码版本管理工具 Git 用起来 01 Git 基础
  19. 北航计算机学院本科优秀毕业论文,我校荣获32项北京市普通高等学校优秀本科生毕业设计(论文)...
  20. 什么是验厂什么是认证

热门文章

  1. 通过集成式有源EMI滤波器降低EMI并缩小电源尺寸
  2. Windows VMware中Ubuntu与宿主共享文件夹
  3. UOJ #53.线段树区间修改
  4. MySQL全面优化,速度飞起来
  5. 【hdu】4521 小明序列【LIS变种】【间隔至少为d】
  6. POJ-2955 Brackets
  7. 在使用Reference Source调试.Net 源代码时如何取消optimizations(代码优化)-翻译
  8. 8位图像的双边滤波器实现
  9. python 第六章 函数 pta(1)
  10. 真香!Vision Transformer 快速实现 Mnist 识别