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