Spring Boot應(yīng)用事件監(jiān)聽示例詳解
前言
本文主要給大家介紹了關(guān)于Spring Boot應(yīng)用事件監(jiān)聽的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧
1. Spring Boot特有的應(yīng)用事件
除了Spring框架的事件,Spring Boot的SpringApplication也發(fā)送了一些自己的事件:
- ApplicationStartingEvent:在任何處理(除了注冊(cè)listener和initializer)開始之前發(fā)送。
- ApplicationEnvironmentPreparedEvent: 在context創(chuàng)建之前,而用到context中的Environment已經(jīng)被識(shí)別時(shí)發(fā)送。
- ApplicationContextInitializedEvent: SpringApplication正在啟動(dòng),ApplicationContext已準(zhǔn)備好且ApplicationContextInitializer已被調(diào)用但是bean的定義還沒有被加載時(shí)發(fā)送。
- ApplicationPreparedEvent: 在context刷新之前,在bean的定義已經(jīng)被加載之后調(diào)用。
- ApplicationStartedEvent: 在任何應(yīng)用和command-line runner調(diào)用之前,而context已經(jīng)被刷新時(shí)發(fā)送。
- ApplicationReadyEvent: 在任何應(yīng)用和command-line runner被調(diào)用的時(shí)候發(fā)送,它意味著應(yīng)用可以接受請(qǐng)求了。
- ApplicationFailedEvent: 在啟動(dòng)時(shí)有異常的時(shí)候發(fā)送。
有些事件是在ApplicationContext創(chuàng)建之前觸發(fā)的,所以我們不能用常規(guī)的注冊(cè)成bean的事件監(jiān)聽方式:
- 注解了@EventListener注解分方法的類注冊(cè)的bean;
- 實(shí)現(xiàn)了ApplicationListener<Event>接口的類注冊(cè)的bean。
像ApplicationStartedEvent和ApplicationReadyEvent是ApplicationContext創(chuàng)建之后觸發(fā)的,可以用上述兩種方式來監(jiān)聽事件。
2. 如何監(jiān)聽這些事件
我們可以通過下面的方式注冊(cè)監(jiān)聽:
2.1. SpringApplication.addListeners(...)
SpringApplication application = new SpringApplication(StartEventsApplication.class); application.addListeners( (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()) ); application.run(args);
2.2. SpringApplicationBuilder.listeners(...)
new SpringApplicationBuilder() .sources(StartEventsApplication.class) .listeners( (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()) ) .run(args);
2.3. META-INF/spring.factories
src/main/resources/META-INF/spring.factories:
org.springframework.context.ApplicationListener=top.wisely.startevents.listeners.ApplicationContextInitializedEventListener, \ top.wisely.startevents.listeners.ApplicationEnvironmentPreparedEventListener, \ top.wisely.startevents.listeners.ApplicationPreparedEventListener, \ top.wisely.startevents.listeners.ApplicationReadyEventListener, \ top.wisely.startevents.listeners.ApplicationStartedEventListener, \ top.wisely.startevents.listeners.ApplicationStartingEventListener
監(jiān)聽器只需實(shí)現(xiàn)ApplicationListener<要監(jiān)聽的接口類型>接口,無需手動(dòng)注冊(cè)為bean:
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()); } }
3. 源碼地址
https://github.com/wiselyman/spring-boot-application-events.git (本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring的事件發(fā)布與監(jiān)聽方式案例講解
今天去官網(wǎng)查看spring?boot資料時(shí),在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼
MinIO?是一款基于Go語言的高性能對(duì)象存儲(chǔ)服務(wù),本文主要介紹了Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03java數(shù)據(jù)庫連接池的特點(diǎn)及步驟
大家好,本篇文章主要講的是數(shù)據(jù)庫連接池的特點(diǎn)及步驟,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式
這篇文章主要介紹了SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Hibernate中Session.get()方法和load()方法的詳細(xì)比較
今天小編就為大家分享一篇關(guān)于Hibernate中Session.get()方法和load()方法的詳細(xì)比較,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03