application.properties中增加
context.listener.classes=demo.springboot.listener.ApplicationEventListener
實現接口ApplicationListener
package demo.springboot.listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextStartedEvent; import org.springframework.context.event.ContextStoppedEvent; /** * springboot 生命周期 * @author ardo */ public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onApplicationEvent(ApplicationEvent event) { // 在這里可以監聽到Spring Boot的生命周期 if(event instanceof ApplicationStartingEvent){ logger.info("應用程序啟動中"); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { // 初始化環境變量 logger.info("初始化環境變量"); } else if (event instanceof ApplicationPreparedEvent) { // 初始化環境變量完成,初始化上下文context完成 logger.info("初始化環境變量完成,初始化上下文context完成"); } else if (event instanceof ContextRefreshedEvent) { // 應用刷新 logger.info("應用刷新"); } else if (event instanceof ApplicationReadyEvent) {// 應用已啟動完成 logger.info("應用已啟動完成"); } else if (event instanceof ContextStartedEvent) { // 應用啟動,Spring2.5新增的事件,當容器調用ConfigurableApplicationContext的 Start()方法開始/重新開始容器時觸發該事件。 logger.info("應用啟動好了"); } else if(event instanceof ApplicationFailedEvent){ logger.info("應用啟動失敗"); }else if (event instanceof ContextStoppedEvent) { // 應用停止,Spring2.5新增的事件,當容器調用ConfigurableApplicationContext 的Stop()方法停止容器時觸發該事件。 logger.info("應用停止"); } else if (event instanceof ContextClosedEvent) { // 應用關閉,當ApplicationContext被關閉時觸發該事件。容器被關閉時,其管理的所有 單例Bean都被銷毀。 logger.info("應用關閉"); } else { logger.info("其他事件" + event.toString()); } } }
ApplicationContextInitializedEvent:當 SpringApplication 啟動並且准備好 ApplicationContext,並且在加載任何 bean 定義之前調用了 ApplicationContextInitializers 時發布的事件。對應的生命周期方法是contextPrepared()。
ApplicationPreparedEvent:它是SpringBoot上下文 context 創建完成是發布的事件;但此時 spring 中的 bean 還沒有完全初始化完成,因為后面還要refreshContext。這里可以將上下文傳遞出去做一些額外的操作。但是在該監聽器中是無法獲取自定義 bean 並進行操作的。對應的生命周期方法是 contextLoaded()。
ApplicationStartedEvent:這個事件是在 2.0 版本才引入的;具體發布是在應用程序上下文刷新之后,調用任何 ApplicationRunner 和 CommandLineRunner 運行程序之前。
SpringApplicationRunListeners:是一個集合類,內部包含一個 log 和包含 SpringApplicationRunListener 的 List。而 SpringApplicationRunListener 主要是監聽 SpringApplication 對象的,里面的方法都定義了在何時調用 SpringApplicationRunListener 的各種方法,它的實現類是EventPublishingRunListener。它就是一個ApplicationListener的代理。springboot啟動的幾個主要過程的監聽通知都是通過他來進行回調,它的生命周期就是從開始啟動,到啟動結束。
下面的每一個方法 SpringApplicationRunListener 都把其包裝成一個事件,在spring容器還未成功 refreshed 之前都是使用SimpleApplicationEventMulticaster 去尋找對該事件感興趣的ApplicationListener,然后調用其onApplicationEvent方法
starting:當SpringApplication對象的run方法剛啟動的時候(依靠SimpleApplicationEventMulticaster),對應的事件:ApplicationStartingEvent
environmentPrepared:在environment Prepared 但是spring容器還未創建的時候(依靠SimpleApplicationEventMulticaster),對應的事件:ApplicationPreparedEvent
contextPrepared:當spring容器已經創建且准備好了,(目前是空的實現),這時候environment資源還沒初始化完成。
contextLoaded:當spring容器已經loaded 且未refreshContext 。這個時候environment資源也初始化完成。load就是將我們的primaryClass注冊到spring容器中,(依靠SimpleApplicationEventMulticaster) 同時將之前獲取到的ApplicationListener都加入到spring容器中,此時如果ApplicationListener還是ApplicationContextAware的也要調用其setApplicationContext方法。對應的事件:ApplicationPreparedEvent
started:spring容器已經刷新過且應用已經啟動,但是CommandLineRunners和ApplicationRunners還未調用,直接通過spring容器自己發送(因為ApplicationListener已經加入spring容器),對應的事件:ContextStartedEvent
running:我們已經調用了CommandLineRunners,直接通過spring容器自己發送(因為ApplicationListener已經加入spring容器),對應的事件:ApplicationReadEvent
failed:當異常發生的時候就調用這個,如果spring容器沒有loaded 或者沒有激活就使用SimpleApplicationEventMulticaster,否則還是依靠spring容器自己。對應該事件:ApplicationFailedEvent