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

spring boot 監(jiān)聽容器啟動(dòng)代碼實(shí)例

 更新時(shí)間:2019年10月16日 16:55:23   作者:匠心編程  
這篇文章主要介紹了spring boot 監(jiān)聽容器啟動(dòng)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在使用Spring框架開發(fā)時(shí), 有時(shí)我們需要在spring容器初始化完成后做一些操作, 那么我們可以通過自定義ApplicationListener 來實(shí)現(xiàn).

自定義監(jiān)聽器

@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

    // 獲取spring 上下文
    ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();

    // do your work...
}

源碼分析

springboot 啟動(dòng)應(yīng)用, 執(zhí)行 SpringApplication.run(String… args) 方法

run方法中執(zhí)行完初始化上下文方法后, 會(huì)執(zhí)行this.refreshContext方法, 刷新

經(jīng)過一堆方法跳轉(zhuǎn), 執(zhí)行 AbstractApplicationContextl類的publishEvent(Object event, @Nullable ResolvableType eventType) 方法

然后執(zhí)行 SimpleApplicationEventMulticaster.multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType)

然后依次執(zhí)行所有監(jiān)聽器的onApplicationEvent()方法

// 遍歷所有ApplicationListeners, 依次執(zhí)行ApplicationListener 的onApplicationEvent()方法
public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) {
  ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
  Iterator var4 = this.getApplicationListeners(event, type).iterator();

  while(var4.hasNext()) {
    ApplicationListener<?> listener = (ApplicationListener)var4.next();
    Executor executor = this.getTaskExecutor();
    if (executor != null) {
      executor.execute(() -> {
        this.invokeListener(listener, event);
      });
    } else {
      this.invokeListener(listener, event);
    }
  }

}

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

相關(guān)文章

最新評(píng)論

普安县| 铜陵市| 云南省| 河西区| 治多县| 游戏| 肇东市| 浮梁县| 定安县| 时尚| 托克逊县| 阿城市| 阜城县| 栾城县| 共和县| 平安县| 全州县| 南召县| 丰城市| 阜新| 临洮县| 安顺市| 京山县| 奈曼旗| 资中县| 修水县| 贡觉县| 三原县| 江山市| 铜川市| 吉水县| 且末县| 滨海县| 大埔区| 绥化市| 松滋市| 松潘县| 铜山县| 嘉善县| 义马市| 德保县|