Spring中的Lifecycle接口使用與源碼分析
LifeCycle接口定義了Spring容器的生命周期,任何被Spring管理的對象都可以實現該接口。當Spring容器本身啟動和停止時,會回調LifeCycle接口中定義的方法。
Lifecycle接口的聲明
org.springframework.context.Lifecycle
public interface Lifecycle {
/**
* 啟動當前組件,如果組件已經在運行,不應該拋出異常,這樣將開始信號傳播到應用的所有組件中去。
*/
void start();
/**
* 通常以同步方式停止該組件,當該方法執(zhí)行完成后,該組件會被完全停止。當需要異步停止行為時,考慮實現SmartLifecycle和它的 stop(Runnable) 方法變體。
注意,此停止通知在銷毀前不能保證到達:
在常規(guī)關閉時,{@code Lifecycle} bean將首先收到一個停止通知,然后才傳播常規(guī)銷毀回調;
在上下文的生命周期內的刷新或中止時,只調用銷毀方法
對于容器,這將把停止信號傳播到應用的所有組件
*/
void stop();
/**
* 檢查此組件是否正在運行。
* 1. 只有該方法返回false時,start方法才會被執(zhí)行。
* 2. 只有該方法返回true時,stop(Runnable callback)或stop()方法才會被執(zhí)行。
*/
boolean isRunning();
}Lifecycle的使用
自定義一個Lifecycle需要實現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;
}
}測試類:
public class LifecycleDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(MyLifecycle.class);
applicationContext.refresh();
}
}啟動main()方法后發(fā)現并沒有調用MyLifecycle的start()和stop()方法。
把測試類改為如下,主動調用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();
}
}運行結果如下:
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'
這時我們看到Spring容器回調了Lifecycle生命周期的方法。
SmartLifecycle接口的聲明
常規(guī)的LifeCycle接口只能在容器上下文顯式的調用start()或stop()方法時,才會去回調LifeCycle的實現類的start()或stop()方法邏輯,并不意味著在容器上下文刷新時自動回調。
org.springframework.context.SmartLifecycle
public interface SmartLifecycle extends Lifecycle, Phased {
int DEFAULT_PHASE = Integer.MAX_VALUE;
/**
* 如果該`Lifecycle`類所在的上下文在調用`refresh`時,希望能夠自己自動進行回調,則返回`true`* ,
* false的值表明組件打算通過顯式的start()調用來啟動,類似于普通的Lifecycle實現。
*/
default boolean isAutoStartup() {
return true;
}
default void stop(Runnable callback) {
stop();
callback.run();
}
@Override
default int getPhase() {
return DEFAULT_PHASE;
}
}SmartLifecycle的使用
自定義SmartLifecycle需要實現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;
}
}測試類:
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();
}
}運行結果如下:
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'
容器中多個實現了Lifecycle的類如果希望有順序的進行回調時,那么啟動和關閉調用的順序可能很重要。如果任何兩個對象之間存在依賴關系,那么依賴方將在依賴后開始,在依賴前停止。然而,有時直接依賴關系是未知的。您可能只知道某個類型的對象應該在另一個類型的對象之前開始。在這些情況下,SmartLifecycle接口定義了另一個選項,即在其超接口上定義的getPhase()方法。
當開始時,getPhase()返回值最小的對象先開始,當停止時,遵循相反的順序。因此,實現SmartLifecycle的對象及其getPhase()方法返回Integer.MIN_VALUE將在第一個開始和最后一個停止。相反,MAX_VALUE將指示對象應該在最后啟動并首先停止。
源碼解讀
Lifecycle的調用時機
Lifecycle中的方法只有在主動調用容器的start()和stop()方法時才會觸發(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;
}最后會調用DefaultLifecycleProcessor的startBeans()方法。
SmartLifecycle的調用時機
SmartLifecycle的調用時機發(fā)生在容器refresh()時。
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)建并調用onRefresh()方法。
org.springframework.context.support.DefaultLifecycleProcessor#onRefresh
public void onRefresh() {
startBeans(true);
this.running = true;
}通用最后會調用DefaultLifecycleProcessor的startBeans()方法,只不過參數傳入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,表示調用的Lifecycle
// autoStartupOnly=true,表示調用的SmartLifecycle
int phase = getPhase(bean);
// 根據phase進行分組
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) {
// 調用start()
phases.get(key).start();
}
}
}到此這篇關于Spring中的Lifecycle接口使用與源碼分析的文章就介紹到這了,更多相關Spring中的Lifecycle接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
GSON實現Java對象的JSON序列化與反序列化的實例教程
GSON是Google開發(fā)并開源的一個Java的JSON轉換庫,這里我們將來講解GSON實現Java對象的JSON序列化與反序列化的實例教程,需要的朋友可以參考下2016-06-06
SpringMVC加載控制與Postmand的使用和Rest風格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實現了Web MVC設計模式,請求驅動類型的輕量級Web框架,即使用了MVC架構模式的思想,將Web層進行職責解耦?;谡埱篁寗又傅木褪鞘褂谜埱?響應模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們日常Web開發(fā)2022-10-10
springboot如何使用redis的incr創(chuàng)建分布式自增id
這篇文章主要介紹了springboot如何使用redis的incr創(chuàng)建分布式自增id,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

