前言
本文主要給大家介紹了關于Spring Boot應用事件監聽的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧
1. Spring Boot特有的應用事件
除了Spring框架的事件,Spring Boot的SpringApplication也發送了一些自己的事件:
有些事件是在ApplicationContext創建之前觸發的,所以我們不能用常規的注冊成bean的事件監聽方式:
像ApplicationStartedEvent和ApplicationReadyEvent是ApplicationContext創建之后觸發的,可以用上述兩種方式來監聽事件。
2. 如何監聽這些事件
我們可以通過下面的方式注冊監聽:
2.1. SpringApplication.addListeners(...)
SpringApplication application = new SpringApplication(StartEventsApplication.class);application.addListeners( (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()));application.run(args);
2.2. SpringApplicationBuilder.listeners(...)
new SpringApplicationBuilder() .sources(StartEventsApplication.class) .listeners( (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()), (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 監聽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
監聽器只需實現ApplicationListener<要監聽的接口類型>接口,無需手動注冊為bean:
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { log.info("----------- 監聽Spring Boot:" + event.getClass().getSimpleName()); }}
3. 源碼地址
https://github.com/wiselyman/spring-boot-application-events.git
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選