蘭州seo快速優(yōu)化報價移動優(yōu)化課主講:夫唯老師
目錄
- 概述
- 實踐
- 監(jiān)聽spring boot ready事件
- 代碼
- 源碼
- 初始化流程
- 調(diào)用流程
- 結(jié)束
概述
spring boot 版本為 2.7.17
。
整體看一下spring
及spring boot
相關(guān)事件。
根據(jù)下文所給的源碼關(guān)鍵處,打上斷點,可以進行快速調(diào)試。降低源碼閱讀難度。
實踐
spring 相關(guān)事件
- 上下文更新事件(ContextRefreshedEvent):該事件會在ApplicationContext更新時發(fā)布。也可以在調(diào)用ConfigurableApplicationContext接口中的refresh()方法時被觸發(fā)。
- 上下文開始事件(ContextStartedEvent):當容器ConfigurableApplicationContext的Start()方法開始/重新開始容器時觸發(fā)該事件。
- 上下文停止事件(ContextStoppedEvent):當容ConfigurableApplicationContext的Stop()方法停止容器時觸發(fā)該事件。
- 上下文關(guān)閉事件(ContextClosedEvent):當ApplicationContext被關(guān)閉時觸發(fā)該事件。容器被關(guān)閉時,其管理的所有單例Bean都被銷毀。
spring boot 相關(guān)事件
- ApplicationStartingEvent :spring boot啟動開始時執(zhí)行的事件
- ApplicationEnvironmentPreparedEvent:spring boot 對應(yīng)Enviroment已經(jīng)準備完畢,但此時上下文context還沒有創(chuàng)建。
- ApplicationPreparedEvent:spring boot上下文context創(chuàng)建完成,但此時spring中的bean是沒有完全加載完成的(org.springframework.boot.SpringApplicationRunListeners#environmentPrepared 這個觸發(fā)加載配置文件)。
- ApplicationFailedEvent:spring boot啟動異常時執(zhí)行事件
監(jiān)聽spring boot ready事件
代碼
@Component
public class SpringBootReadyListener implements ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {System.out.println("....ready..");}
}
窗口執(zhí)行結(jié)果
源碼
初始化流程
org.springframework.boot.SpringApplicationRunListener 很重要的接口,后面spring boot 發(fā)事件消息,使用這個接口的實現(xiàn)類 org.springframework.boot.context.event.EventPublishingRunListener 來執(zhí)行。
org.springframework.boot.SpringApplication#getRunListeners
調(diào)用流程
org.springframework.boot.SpringApplication#run(java.lang.Class<?>[], java.lang.String[])
org.springframework.boot.SpringApplication#run(java.lang.String...)
org.springframework.boot.SpringApplicationRunListeners#ready
org.springframework.boot.SpringApplicationRunListeners#doWithListeners(java.lang.String, java.util.function.Consumer<org.springframework.boot.SpringApplicationRunListener>, java.util.function.Consumer<org.springframework.core.metrics.StartupStep>)
org.springframework.boot.context.event.EventPublishingRunListener#ready
org.springframework.context.support.AbstractApplicationContext#publishEvent(java.lang.Object, org.springframework.core.ResolvableType)
org.springframework.context.support.AbstractApplicationContext#getApplicationEventMulticaster
org.springframework.context.event.SimpleApplicationEventMulticaster#invokeListener
org.springframework.context.event.SimpleApplicationEventMulticaster#doInvokeListener
發(fā)送啟動 ready 事件消息。
執(zhí)行結(jié)束
結(jié)束
根據(jù)上文所給的源碼關(guān)鍵處,打上斷點,可以進行快速調(diào)試。降低源碼閱讀難度。