Spring中的Lifecycle接口使用與源碼分析
LifeCycle接口定義了Spring容器的生命周期,任何被Spring管理的對(duì)象都可以實(shí)現(xiàn)該接口。當(dāng)Spring容器本身啟動(dòng)和停止時(shí),會(huì)回調(diào)LifeCycle接口中定義的方法。
Lifecycle接口的聲明
org.springframework.context.Lifecycle
public interface Lifecycle { /** * 啟動(dòng)當(dāng)前組件,如果組件已經(jīng)在運(yùn)行,不應(yīng)該拋出異常,這樣將開始信號(hào)傳播到應(yīng)用的所有組件中去。 */ void start(); /** * 通常以同步方式停止該組件,當(dāng)該方法執(zhí)行完成后,該組件會(huì)被完全停止。當(dāng)需要異步停止行為時(shí),考慮實(shí)現(xiàn)SmartLifecycle和它的 stop(Runnable) 方法變體。 注意,此停止通知在銷毀前不能保證到達(dá): 在常規(guī)關(guān)閉時(shí),{@code Lifecycle} bean將首先收到一個(gè)停止通知,然后才傳播常規(guī)銷毀回調(diào); 在上下文的生命周期內(nèi)的刷新或中止時(shí),只調(diào)用銷毀方法 對(duì)于容器,這將把停止信號(hào)傳播到應(yīng)用的所有組件 */ void stop(); /** * 檢查此組件是否正在運(yùn)行。 * 1. 只有該方法返回false時(shí),start方法才會(huì)被執(zhí)行。 * 2. 只有該方法返回true時(shí),stop(Runnable callback)或stop()方法才會(huì)被執(zhí)行。 */ boolean isRunning(); }
Lifecycle的使用
自定義一個(gè)Lifecycle需要實(shí)現(xiàn)Lifecycle接口:
package com.morris.spring.demo.lifecycle; import org.springframework.context.Lifecycle; /** * 自定義Lifecycle */ public class MyLifecycle implements Lifecycle { private boolean isRunning; @Override public void start() { System.out.println("MyLifecycle start"); isRunning = true; } @Override public void stop() { System.out.println("MyLifecycle stop"); isRunning = false; } @Override public boolean isRunning() { return isRunning; } }
測(cè)試類:
public class LifecycleDemo { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(MyLifecycle.class); applicationContext.refresh(); } }
啟動(dòng)main()方法后發(fā)現(xiàn)并沒有調(diào)用MyLifecycle的start()和stop()方法。
把測(cè)試類改為如下,主動(dòng)調(diào)用applicationContext容器的start()和stop()方法:
public class LifecycleDemo { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(MyLifecycle.class); applicationContext.refresh(); applicationContext.start(); applicationContext.stop(); } }
運(yùn)行結(jié)果如下:
DEBUG main DefaultLifecycleProcessor:356 - Starting beans in phase 0 MyLifecycle start DEBUG main DefaultLifecycleProcessor:188 - Successfully started bean 'myLifecycle' DEBUG main DefaultLifecycleProcessor:369 - Stopping beans in phase 0 MyLifecycle stop DEBUG main DefaultLifecycleProcessor:253 - Successfully stopped bean 'myLifecycle'
這時(shí)我們看到Spring容器回調(diào)了Lifecycle生命周期的方法。
SmartLifecycle接口的聲明
常規(guī)的LifeCycle接口只能在容器上下文顯式的調(diào)用start()或stop()方法時(shí),才會(huì)去回調(diào)LifeCycle的實(shí)現(xiàn)類的start()或stop()方法邏輯,并不意味著在容器上下文刷新時(shí)自動(dòng)回調(diào)。
org.springframework.context.SmartLifecycle
public interface SmartLifecycle extends Lifecycle, Phased { int DEFAULT_PHASE = Integer.MAX_VALUE; /** * 如果該`Lifecycle`類所在的上下文在調(diào)用`refresh`時(shí),希望能夠自己自動(dòng)進(jìn)行回調(diào),則返回`true`* , * false的值表明組件打算通過顯式的start()調(diào)用來啟動(dòng),類似于普通的Lifecycle實(shí)現(xiàn)。 */ default boolean isAutoStartup() { return true; } default void stop(Runnable callback) { stop(); callback.run(); } @Override default int getPhase() { return DEFAULT_PHASE; } }
SmartLifecycle的使用
自定義SmartLifecycle需要實(shí)現(xiàn)SmartLifecycle接口:
package com.morris.spring.demo.lifecycle; import org.springframework.context.SmartLifecycle; /** * 自定義SmartLifecycle */ public class MySmartLifecycle implements SmartLifecycle { private boolean isRunning; @Override public void start() { System.out.println("MyLifecycle start"); isRunning = true; } @Override public void stop() { System.out.println("MyLifecycle stop"); isRunning = false; } @Override public boolean isRunning() { return isRunning; } }
測(cè)試類:
package com.morris.spring.demo.lifecycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * 演示SmartLifecycle */ public class SmartLifecycleDemo { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(MySmartLifecycle.class); applicationContext.refresh(); } }
運(yùn)行結(jié)果如下:
DEBUG main DefaultListableBeanFactory:228 - Creating shared instance of singleton bean 'mySmartLifecycle' DEBUG main DefaultLifecycleProcessor:357 - Starting beans in phase 2147483647 MyLifecycle start DEBUG main DefaultLifecycleProcessor:189 - Successfully started bean 'mySmartLifecycle'
容器中多個(gè)實(shí)現(xiàn)了Lifecycle的類如果希望有順序的進(jìn)行回調(diào)時(shí),那么啟動(dòng)和關(guān)閉調(diào)用的順序可能很重要。如果任何兩個(gè)對(duì)象之間存在依賴關(guān)系,那么依賴方將在依賴后開始,在依賴前停止。然而,有時(shí)直接依賴關(guān)系是未知的。您可能只知道某個(gè)類型的對(duì)象應(yīng)該在另一個(gè)類型的對(duì)象之前開始。在這些情況下,SmartLifecycle接口定義了另一個(gè)選項(xiàng),即在其超接口上定義的getPhase()方法。
當(dāng)開始時(shí),getPhase()返回值最小的對(duì)象先開始,當(dāng)停止時(shí),遵循相反的順序。因此,實(shí)現(xiàn)SmartLifecycle的對(duì)象及其getPhase()方法返回Integer.MIN_VALUE將在第一個(gè)開始和最后一個(gè)停止。相反,MAX_VALUE將指示對(duì)象應(yīng)該在最后啟動(dòng)并首先停止。
源碼解讀
Lifecycle的調(diào)用時(shí)機(jī)
Lifecycle中的方法只有在主動(dòng)調(diào)用容器的start()和stop()方法時(shí)才會(huì)觸發(fā),所以直接看容器的start()或stop()方法即可。
org.springframework.context.support.AbstractApplicationContext#start
public void start() { // 獲取DefaultLifecycleProcessor /** * @see DefaultLifecycleProcessor#start() */ getLifecycleProcessor().start(); publishEvent(new ContextStartedEvent(this)); }
org.springframework.context.support.DefaultLifecycleProcessor#start
public void start() { startBeans(false); this.running = true; }
最后會(huì)調(diào)用DefaultLifecycleProcessor的startBeans()方法。
SmartLifecycle的調(diào)用時(shí)機(jī)
SmartLifecycle的調(diào)用時(shí)機(jī)發(fā)生在容器refresh()時(shí)。
org.springframework.context.support.AbstractApplicationContext#finishRefresh
protected void finishRefresh() { // Clear context-level resource caches (such as ASM metadata from scanning). clearResourceCaches(); // Initialize lifecycle processor for this context. // 注入DefaultLifecycleProcessor initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
DefaultLifecycleProcessor的創(chuàng)建并調(diào)用onRefresh()方法。
org.springframework.context.support.DefaultLifecycleProcessor#onRefresh
public void onRefresh() { startBeans(true); this.running = true; }
通用最后會(huì)調(diào)用DefaultLifecycleProcessor的startBeans()方法,只不過參數(shù)傳入true。
DefaultLifecycleProcessor.startBeans()
private void startBeans(boolean autoStartupOnly) { // 拿到容器中所有的Lifecycle Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans(); Map<Integer, LifecycleGroup> phases = new HashMap<>(); lifecycleBeans.forEach((beanName, bean) -> { if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) { // autoStartupOnly=false,表示調(diào)用的Lifecycle // autoStartupOnly=true,表示調(diào)用的SmartLifecycle int phase = getPhase(bean); // 根據(jù)phase進(jìn)行分組 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) { // 調(diào)用start() phases.get(key).start(); } } }
到此這篇關(guān)于Spring中的Lifecycle接口使用與源碼分析的文章就介紹到這了,更多相關(guān)Spring中的Lifecycle接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 非對(duì)稱加密算法RSA實(shí)現(xiàn)詳解
這篇文章主要介紹了java 非對(duì)稱加密算法RSA實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07支持SpEL表達(dá)式的自定義日志注解@SysLog介紹
這篇文章主要介紹了支持SpEL表達(dá)式的自定義日志注解@SysLog,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例,感興趣的小伙伴們可以參考一下2016-05-05通過java備份恢復(fù)mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼
這篇文章主要介紹了如何通過java備份恢復(fù)mysql數(shù)據(jù)庫,其實(shí)一般情況下通過bat或sh就可以,這里主要是介紹了java的實(shí)現(xiàn)思路,喜歡的朋友可以參考下2013-09-09GSON實(shí)現(xiàn)Java對(duì)象的JSON序列化與反序列化的實(shí)例教程
GSON是Google開發(fā)并開源的一個(gè)Java的JSON轉(zhuǎn)換庫,這里我們將來講解GSON實(shí)現(xiàn)Java對(duì)象的JSON序列化與反序列化的實(shí)例教程,需要的朋友可以參考下2016-06-06Java基礎(chǔ)知識(shí)之成員變量和局部變量淺顯易懂總結(jié)
從語法形式上,看成員變量是屬于類的,而局部變量是在方法中定義的變量或是方法的參數(shù);成員變量可以被public,private,static等修飾符所修飾,而局部變量不能被訪問控制修飾符及static所修飾2021-09-09SpringMVC加載控制與Postmand的使用和Rest風(fēng)格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡?qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡(jiǎn)化開發(fā),SpringMVC也是要簡(jiǎn)化我們?nèi)粘eb開發(fā)2022-10-10springboot如何使用redis的incr創(chuàng)建分布式自增id
這篇文章主要介紹了springboot如何使用redis的incr創(chuàng)建分布式自增id,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11