亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot之SpringApplication 事件監(jiān)聽

 更新時(shí)間:2019年03月14日 08:27:52   作者:posuoren  
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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ā)布
ApplicationStartedEvent context 更新之后,任何應(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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用)

    基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用)

    這篇文章主要介紹了基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(60)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(60)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • 基于UncategorizedSQLException異常處理方案

    基于UncategorizedSQLException異常處理方案

    這篇文章主要介紹了基于UncategorizedSQLException異常處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringBoot中controller深層詳細(xì)講解

    SpringBoot中controller深層詳細(xì)講解

    這篇文章主要介紹了SpringBoot在Controller層接收參數(shù)的常用方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • Java詳細(xì)分析講解泛型

    Java詳細(xì)分析講解泛型

    在正式進(jìn)入內(nèi)容之前說明一下:泛型的內(nèi)容太多,也太復(fù)雜。這里因?yàn)镴ava中寫數(shù)據(jù)結(jié)構(gòu)的時(shí)候會(huì)使用到,所以加上。關(guān)于泛型我找了挺多文章,再結(jié)合自己的理解,盡可能將其講清楚。不求會(huì)使用泛型,只要求后面數(shù)據(jù)結(jié)構(gòu)出現(xiàn)泛型的時(shí)候能夠知道是在干什么即可
    2022-05-05
  • Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語(yǔ)國(guó)際化的示例代碼

    Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語(yǔ)國(guó)際化的示例代碼

    這篇文章主要介紹了Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語(yǔ)國(guó)際化的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • SpringBoot調(diào)用第三方WebService接口的兩種方法

    SpringBoot調(diào)用第三方WebService接口的兩種方法

    本文主要介紹了SpringBoot調(diào)用第三方WebService接口的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java中使用回調(diào)函數(shù)的方法實(shí)例

    Java中使用回調(diào)函數(shù)的方法實(shí)例

    本文主要介紹了Java中使用回調(diào)函數(shù)的方法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java中File類中常用方法詳解

    Java中File類中常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了File類中常用方法的程序演示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • SpringBoot整合BCrypt實(shí)現(xiàn)密碼加密

    SpringBoot整合BCrypt實(shí)現(xiàn)密碼加密

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合BCrypt進(jìn)行密碼加密,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論