溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring boot中怎么使用SpringApplication實(shí)現(xiàn)事件監(jiān)聽

發(fā)布時(shí)間:2021-06-11 15:31:56 來源:億速云 閱讀:132 作者:Leah 欄目:編程語言

spring boot中怎么使用SpringApplication實(shí)現(xiàn)事件監(jiān)聽,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

spring application listener

在 spring 框架中,有多種事件, 這些時(shí)間會(huì)在不同的運(yùn)行時(shí)刻發(fā)布,來通知監(jiān)聽者。本文僅僅介紹 SpringApplicationEvent 的事件的監(jiān)聽。

事件類型

EventType發(fā)布時(shí)間
ApplicationContextInitializedEvent在 SpringApplication正在啟動(dòng), ApplicationContext 已經(jīng)準(zhǔn)備好了,ApplicationContextInitializers 被調(diào)用, bean definitions 被加載之前
ApplicationStartingEvent在一次啟動(dòng)之前發(fā)布
ApplicationEnvironmentPreparedEvent在 Environment 準(zhǔn)備好之后,會(huì)有 context 去使用這一 Environment, 會(huì)在 context 創(chuàng)建之前發(fā)出
ApplicationPreparedEvent會(huì)在 bean definitions 加載之后,refresh 之前發(fā)布
ApplicationStartedEventcontext 更新之后,任何應(yīng)用或命令行啟動(dòng)調(diào)用之前
ApplicationReadyEvent任何應(yīng)用或命令行啟動(dòng)調(diào)用之后發(fā)布,說明應(yīng)用已經(jīng)可以被請(qǐng)求了
ApplicationFailedEvent啟動(dòng)發(fā)生有異常時(shí)發(fā)步

如何監(jiān)聽

監(jiān)聽器需要使用 org.springframework.context.ApplicationListener 這個(gè)接口的實(shí)例, 其聲明如下:

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
  /**
  * Handle an application event. * @param event the event to respond to
  */ 
 void onApplicationEvent(E event);
}

需要使用 SpringApplication.addListeners(…) 或 SpringApplicationBuilder.listeners(…) 來添加監(jiān)聽器。也可以在 META-INF/spring.factories 文件中配置:org.springframework.context.ApplicationListener=com.example.project.MyListener。

例子:

public class StartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
 @Override
 public void onApplicationEvent(ApplicationStartingEvent applicationStartingEvent) {
  System.out.println("called own starting listener");

  System.out.println(applicationStartingEvent.getClass());
 }
}
@SpringBootApplication
public class DemoApplication {
 public static void main(String[] args){
  SpringApplication application = new SpringApplication(DemoApplication.class);
  application.addListeners(new StartingEventListener());
  application.run(args);
 }
}

終端運(yùn)行 jar 包:

$ java -jar build/libs/springlisteners-0.0.1-SNAPSHOT.jar
called own starting listener
class org.springframework.boot.context.event.ApplicationStartingEvent

 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.1.3.RELEASE)

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI