前言
每次有人问起SpringBoot的启动顺序是不是又来翻博客了?其实只需要稍微查看Spring的源码即可
步骤
-
SpringBoot的入口
org.springframework.boot.SpringApplication#run(String... args)
,
这里面实现了SpringBoot程序启动的所有步骤 -
启动事件的顺序可以看监听器的顺序定义:
org.springframework.boot.SpringApplicationRunListeners
或者org.springframework.boot.SpringApplicationRunListener
附录
SpringBoot启动顺序
创建组件 | 发送事件 | 执行回调 |
---|---|---|
DefaultBootstrapContext | BootstrapRegistryInitializer#initialize | |
SpringApplicationRunListeners | ||
ApplicationStartingEvent | ||
ConfigurableEnvironment | ||
EnvironmentPreparedEvent | ||
Banner | ||
ConfigurableApplicationContext | ||
ContextPreparedEvent | ||
ConfigurableApplicationContext#refresh | ||
ContextRefreshedEvent | ||
ApplicationStartedEvent | ||
AvailabilityChangeEvent (LivenessState.CORRECT) | ||
ApplicationRunner#run | ||
CommandLineRunner#run | ||
ApplicationReadyEvent | ||
AvailabilityChangeEvent (ReadinessState.ACCEPTING_TRAFFIC) |
提示:可通过ApplicationAvailabilityBean获取到当前LivenessState和ReadinessState的值来判断SpringBoot是否可以启动