最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

spring application listener

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

事件類(lèi)型

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ā)布,說(shuō)明應(yīng)用已經(jīng)可以被請(qǐng)求了
ApplicationFailedEvent 啟動(dòng)發(fā)生有異常時(shí)發(fā)步

如何監(jiān)聽(tīng)

監(jiān)聽(tīng)器需要使用 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(…​) 來(lái)添加監(jiān)聽(tīng)器。也可以在 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)文章

最新評(píng)論

资溪县| 美姑县| 玉树县| 中西区| 吕梁市| 东安县| 河北区| 宿州市| 闽清县| 兴化市| 嘉荫县| 怀宁县| 枞阳县| 张北县| 洞头县| 内黄县| 皮山县| 保定市| 鄂伦春自治旗| 辽阳市| 福海县| 化德县| 宣汉县| 延边| 海兴县| 宿迁市| 石嘴山市| 广灵县| 英德市| 青州市| 鄂托克旗| 临夏县| 错那县| 昆明市| 武宣县| 田东县| 淳安县| 怀化市| 剑河县| 淄博市| 衡阳县|