SpringBoot各个监听器启动执行顺序

简介

了解各个监听器执行顺序,可以让我们了解SpringBoot的启动过程,根据业务在不通节点插入想要实现的代码。比如说服务器启动后加载一些缓存,或者动态修改环境变量中的参数等。

各个监听器方法执行顺序图

请添加图片描述

代码验证

搭建项目

创建一个SpringBoot项目,并添加各个监听器打印日志来验证,关闭其他日志。
项目地址:https://gitee.com/zhaojiyuan/spring-boot-life-cycle
在这里插入图片描述

  1. 通过spring.factories配置Bootstrapper、SpringApplicationRunListener、ApplicationContextInitializer
  2. 通过组件配置ApplicationRunner、CommandLineRunner
启动项目输出日志
方法名:Bootstrapper.intitialize
描述信息:启动载入器

执行顺序:2
方法名:SpringApplicationRunListener.starting
描述信息:Spring应用程序开始启动

执行顺序:3
方法名:SpringApplicationRunListener.environmentPrepared
描述信息:Spring应用程序环境已经准备完毕


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

执行顺序:4
方法名:ApplicationContextInitializer.initialize
描述信息:应用程序上下文初始化

执行顺序:5
方法名:SpringApplicationRunListener.contextPrepared
描述信息:Spring应用程序上下文准备完毕

Starting AppBootStrap using Java 1.8.0_131 on zhaojiyuan with PID 7996 (E:\workSpace\java\idea\private\springBootLifeCycle\build\classes\java\main started by Administrator in E:\workSpace\java\idea\private\springBootLifeCycle)
Running with Spring Boot v2.4.2, Spring v5.3.3
No active profile set, falling back to default profiles: default
执行顺序:6
方法名:SpringApplicationRunListener.contextLoaded
描述信息:Spring应用程序上下文已加载

四月 18, 2022 12:34:08 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8801"]
四月 18, 2022 12:34:08 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Tomcat]
四月 18, 2022 12:34:08 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet engine: [Apache Tomcat/9.0.41]
四月 18, 2022 12:34:08 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring embedded WebApplicationContext
四月 18, 2022 12:34:09 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8801"]
Started AppBootStrap in 1.035 seconds (JVM running for 1.833)
执行顺序:7
方法名:SpringApplicationRunListener.started
描述信息:Spring应用程序启动完毕

执行顺序:8
方法名:ApplicationRunner.run
描述信息:程序启动完毕,可以把应用程序启动后就执行的操作在这里实现

执行顺序:9
方法名:CommandLineRunner.run
描述信息:程序启动完毕,可以把应用程序启动后就执行的操作在这里实现(在ApplicationRunner后执行)

执行顺序:10
方法名:SpringApplicationRunListener.running
描述信息:Spring应用程序运行中

问题

  1. SpringApplicationRunListener.starting()方法的日志打印不输出
    还在研究中,期待有人能帮我解答下,目前debug的原因是config.getFilter()=“”,如果config.getFilter() =null,便可打印日志
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值