国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

Spring Boot應(yīng)用事件監(jiān)聽(tīng)示例詳解

2024-07-14 08:43:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

本文主要給大家介紹了關(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ā)送了一些自己的事件:

  • ApplicationStartingEvent:在任何處理(除了注冊(cè)listener和initializer)開(kāi)始之前發(fā)送。
  • ApplicationEnvironmentPreparedEvent: 在context創(chuàng)建之前,而用到context中的Environment已經(jīng)被識(shí)別時(shí)發(fā)送。
  • ApplicationContextInitializedEvent: SpringApplication正在啟動(dòng),ApplicationContext已準(zhǔn)備好且ApplicationContextInitializer已被調(diào)用但是bean的定義還沒(méi)有被加載時(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)聽(tīng)方式:

  • 注解了@EventListener注解分方法的類注冊(cè)的bean;
  • 實(shí)現(xiàn)了ApplicationListener<Event>接口的類注冊(cè)的bean。

像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)的支持。 


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沅陵县| 阿克苏市| 大田县| 鄂温| 沛县| 平顶山市| 平顺县| 张家港市| 五原县| 绥棱县| 东兴市| 尤溪县| 皮山县| 定边县| 徐闻县| 武夷山市| 南康市| 江门市| 阿拉善左旗| 富源县| 望奎县| 阜阳市| 田东县| 肃宁县| 绍兴市| 通榆县| 金坛市| 梧州市| 长顺县| 尼玛县| 正镶白旗| 甘洛县| 嘉义县| 灵武市| 博客| 梓潼县| 赣榆县| 田林县| 曲靖市| 南京市| 宜城市|