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

SpringBoot中SmartLifecycle的使用解析

 更新時(shí)間:2023年11月15日 08:48:12   作者:阿孟呀  
這篇文章主要介紹了SpringBoot中SmartLifecycle的使用解析,SmartLifecycle是一個(gè)擴(kuò)展了Lifecycle接口,可以跟蹤spring容器ApplicationContext刷新或者關(guān)閉的接口,實(shí)現(xiàn)該接口的實(shí)現(xiàn)類有特定的執(zhí)行順序,需要的朋友可以參考下

前言

SmartLifecycle是一個(gè)擴(kuò)展了Lifecycle接口,可以跟蹤spring容器ApplicationContext刷新或者關(guān)閉的接口,實(shí)現(xiàn)該接口的實(shí)現(xiàn)類有特定的執(zhí)行順序。

當(dāng)ApplicationContext刷新時(shí),所有bean都加載和初始化完成后,根據(jù)isAutoStartup和isRunning判斷是否自動(dòng)執(zhí)行start()

1.新建TestSmartLifecycle類

public class TestSmartLifecycle implements SmartLifecycle {
   
  @Override
    public void start() {
        System.out.println("TestSmartLifecycle is start");
    }
//SmartLifecycle組件停止后的回調(diào),程序被異步關(guān)閉
    @Override
    public void stop() {
        System.out.println("SmartLifecycle is stop");
    }
//為false,并且isAutoStartup為true,執(zhí)行start方法
//為true,執(zhí)行stop方法。
    @Override
    public boolean isRunning() {
        return false;
    }
//如果為true,實(shí)現(xiàn)了SmartLifecycle 接口的組件,能再ApplicationContext已經(jīng)刷新的時(shí)候,自動(dòng)執(zhí)行start方法,
    @Override
    public boolean isAutoStartup() {
        return true;
    }
//當(dāng)running為true時(shí),再應(yīng)用關(guān)閉時(shí)會(huì)執(zhí)行stop(Runnable callback)方法,
如果再執(zhí)行完stop后,running沒有設(shè)置為false時(shí),應(yīng)用會(huì)卡住一段時(shí)間,無法退出
    @Override
    public void stop(Runnable callback) {
        System.out.println("TestSmartLifecycle is stop");
        callback.run();
    }
 
    @Override
    public int getPhase() {
        return 0;
    }
}

2.配置Bean

@Configuration
public class TestConfiguration {
 
    @Bean
    TestSmartLifecycle testSmartLifecycle(){
        return new TestSmartLifecycle();
    }
}

3.debugger啟動(dòng)應(yīng)用程序

可以看見如下的調(diào)用鏈,大致是SpringApplication.run(CommonsTestApplication.class, args)-->refreshContext-->refresh-->startBeans-->start,其中,再startBean中獲取了所有實(shí)現(xiàn)SmartLifecycle的非懶加載的bean,并再start方法中,調(diào)用對(duì)應(yīng)實(shí)現(xiàn)bean的start方法,執(zhí)行相應(yīng)的邏輯,具體可看下面截圖。

private void startBeans(boolean autoStartupOnly) {
		Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
		Map<Integer, LifecycleGroup> phases = new HashMap<>();
		lifecycleBeans.forEach((beanName, bean) -> {
			if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
				int phase = getPhase(bean);
				LifecycleGroup group = phases.get(phase);
				if (group == null) {
					group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
					phases.put(phase, group);
				}
				group.add(beanName, bean);
			}
		});
		if (!phases.isEmpty()) {
			List<Integer> keys = new ArrayList<>(phases.keySet());
			Collections.sort(keys);
			for (Integer key : keys) {
				phases.get(key).start();
			}
		}
	}

到此這篇關(guān)于SpringBoot中SmartLifecycle的使用解析的文章就介紹到這了,更多相關(guān)SmartLifecycle的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

双峰县| 阿拉善盟| 鹤峰县| 广水市| 临泽县| 沧源| 介休市| 永登县| 萨迦县| 沧州市| 安远县| 阿拉尔市| 武功县| 罗源县| 库伦旗| 隆化县| 南平市| 那坡县| 沧州市| 宜兰市| 久治县| 繁昌县| 佛冈县| 大理市| 江城| 廊坊市| 诸城市| 高平市| 义乌市| 泰州市| 泸西县| 淳安县| 嘉禾县| 嫩江县| 布尔津县| 会同县| 隆化县| 浦江县| 信阳市| 佳木斯市| 吴桥县|