前言
每次有人問起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是否可以啟動