您好,登錄后才能下訂單哦!
使用Spring Boot怎么監(jiān)聽應(yīng)用事件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1. Spring Boot特有的應(yīng)用事件
除了Spring框架的事件,Spring Boot的SpringApplication也發(fā)送了一些自己的事件:
ApplicationStartingEvent:在任何處理(除了注冊listener和initializer)開始之前發(fā)送。
ApplicationEnvironmentPreparedEvent: 在context創(chuàng)建之前,而用到context中的Environment已經(jīng)被識別時發(fā)送。
ApplicationContextInitializedEvent: SpringApplication正在啟動,ApplicationContext已準備好且ApplicationContextInitializer已被調(diào)用但是bean的定義還沒有被加載時發(fā)送。
ApplicationPreparedEvent: 在context刷新之前,在bean的定義已經(jīng)被加載之后調(diào)用。
ApplicationStartedEvent: 在任何應(yīng)用和command-line runner調(diào)用之前,而context已經(jīng)被刷新時發(fā)送。
ApplicationReadyEvent: 在任何應(yīng)用和command-line runner被調(diào)用的時候發(fā)送,它意味著應(yīng)用可以接受請求了。
ApplicationFailedEvent: 在啟動時有異常的時候發(fā)送。
有些事件是在ApplicationContext創(chuàng)建之前觸發(fā)的,所以我們不能用常規(guī)的注冊成bean的事件監(jiān)聽方式:
注解了@EventListener注解分方法的類注冊的bean;
實現(xiàn)了ApplicationListener<Event>接口的類注冊的bean。
像ApplicationStartedEvent和ApplicationReadyEvent是ApplicationContext創(chuàng)建之后觸發(fā)的,可以用上述兩種方式來監(jiān)聽事件。
2. 如何監(jiān)聽這些事件
我們可以通過下面的方式注冊監(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)聽器只需實現(xiàn)ApplicationListener<要監(jiān)聽的接口類型>接口,無需手動注冊為bean:
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { log.info("----------- 監(jiān)聽Spring Boot:" + event.getClass().getSimpleName()); } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。