SpringBoot內置生命周期事件詳解 SpringBoot源碼(十)

SpringBoot中文注釋項目Github地址:

https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE

本篇接 SpringBoot事件監聽機制源碼分析(上) SpringBoot源碼(九)

1 溫故而知新

溫故而知新,我們來簡單回顧一下上篇的內容,上一篇我們分析了SpringBoot啟動時廣播生命周期事件的原理,現將關鍵步驟再濃縮總結下:

  1. 為廣播SpringBoot內置生命周期事件做前期準備:1)首先加載ApplicationListener監聽器實現類;2)其次加載SPI擴展類EventPublishingRunListener
  2. SpringBoot啟動時利用EventPublishingRunListener廣播生命周期事件,然后ApplicationListener監聽器實現類監聽相應的生命周期事件執行一些初始化邏輯的工作。

2 引言

上篇文章的側重點是分析了SpringBoot啟動時廣播生命周期事件的原理,此篇文章我們再來詳細分析SpringBoot內置的7種生命周期事件的源碼。

3 SpringBoot生命周期事件源碼分析

分析SpringBoot的生命周期事件,我們先來看一張類結構圖:



由上圖可以看到事件類之間的關系:

  1. 最頂級的父類是JDK的事件基類EventObject
  2. 然后Spring的事件基類ApplicationEvent繼承了JDK的事件基類EventObject;
  3. 其次SpringBoot的生命周期事件基類SpringApplicationEvent繼承了Spring的事件基類ApplicationEvent;
  4. 最后SpringBoot具體的7個生命周期事件類再繼承了SpringBoot的生命周期事件基類SpringApplicationEvent

3.1 JDK的事件基類EventObject

EventObject類是JDK的事件基類,可以說是所有Java事件類的基本,即所有的Java事件類都直接或間接繼承于該類,源碼如下:

// EventObject.java

public class EventObject implements java.io.Serializable {

    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object  source;
    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");
        this.source = source;
    }
    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
        return source;
    }
    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}

可以看到EventObject類只有一個屬性source,這個屬性是用來記錄最初事件是發生在哪個類,舉個栗子,比如在SpringBoot啟動過程中會發射ApplicationStartingEvent事件,而這個事件最初是在SpringApplication類中發射的,因此source就是SpringApplication對象。

3.2 Spring的事件基類ApplicationEvent

ApplicationEvent繼承了DK的事件基類EventObject類,是Spring的事件基類,被所有Spring的具體事件類繼承,源碼如下:

// ApplicationEvent.java

/**
 * Class to be extended by all application events. Abstract as it
 * doesn't make sense for generic events to be published directly.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 */
public abstract class ApplicationEvent extends EventObject {
    /** use serialVersionUID from Spring 1.2 for interoperability. */
    private static final long serialVersionUID = 7099057708183571937L;
    /** System time when the event happened. */
    private final long timestamp;
    /**
     * Create a new ApplicationEvent.
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public ApplicationEvent(Object source) {
        super(source);
        this.timestamp = System.currentTimeMillis();
    }
    /**
     * Return the system time in milliseconds when the event happened.
     */
    public final long getTimestamp() {
        return this.timestamp;
    }
}

可以看到ApplicationEvent有且僅有一個屬性timestamp,該屬性是用來記錄事件發生的時間。

3.3 SpringBoot的事件基類SpringApplicationEvent

SpringApplicationEvent類繼承了Spring的事件基類ApplicationEvent,是所有SpringBoot內置生命周期事件的父類,源碼如下:


/**
 * Base class for {@link ApplicationEvent} related to a {@link SpringApplication}.
 *
 * @author Phillip Webb
 */
@SuppressWarnings("serial")
public abstract class SpringApplicationEvent extends ApplicationEvent {
    private final String[] args;
    public SpringApplicationEvent(SpringApplication application, String[] args) {
        super(application);
        this.args = args;
    }
    public SpringApplication getSpringApplication() {
        return (SpringApplication) getSource();
    }
    public final String[] getArgs() {
        return this.args;
    }
}

可以看到SpringApplicationEvent有且僅有一個屬性args,該屬性就是SpringBoot啟動時的命令行參數即標注@SpringBootApplication啟動類中main函數的參數。

3.4 SpringBoot具體的生命周期事件類

接下來我們再來看一下SpringBoot內置生命周期事件即SpringApplicationEvent的具體子類們。

3.4.1 ApplicationStartingEvent

// ApplicationStartingEvent.java

public class ApplicationStartingEvent extends SpringApplicationEvent {
    public ApplicationStartingEvent(SpringApplication application, String[] args) {
        super(application, args);
    }
}

SpringBoot開始啟動時便會發布ApplicationStartingEvent事件,其發布時機在環境變量Environment或容器ApplicationContext創建前但在注冊ApplicationListener具體監聽器之后,標志標志SpringApplication開始啟動。

3.4.2 ApplicationEnvironmentPreparedEvent

// ApplicationEnvironmentPreparedEvent.java

public class ApplicationEnvironmentPreparedEvent extends SpringApplicationEvent {
    private final ConfigurableEnvironment environment;
    /**
     * Create a new {@link ApplicationEnvironmentPreparedEvent} instance.
     * @param application the current application
     * @param args the arguments the application is running with
     * @param environment the environment that was just created
     */
    public ApplicationEnvironmentPreparedEvent(SpringApplication application,
            String[] args, ConfigurableEnvironment environment) {
        super(application, args);
        this.environment = environment;
    }
    /**
     * Return the environment.
     * @return the environment
     */
    public ConfigurableEnvironment getEnvironment() {
        return this.environment;
    }
}

可以看到ApplicationEnvironmentPreparedEvent事件多了一個environment屬性,我們不妨想一下,多了environment屬性的作用是啥?
答案就是ApplicationEnvironmentPreparedEvent事件的environment屬性作用是利用事件發布訂閱機制,相應監聽器們可以從ApplicationEnvironmentPreparedEvent事件中取出environment變量,然后我們可以為environment屬性增加屬性值或讀出environment變量中的值。

舉個栗子: ConfigFileApplicationListener監聽器就是監聽了ApplicationEnvironmentPreparedEvent事件,然后取出ApplicationEnvironmentPreparedEvent事件的environment屬性,然后再為environment屬性增加application.properties配置文件中的環境變量值。

當SpringApplication已經開始啟動且環境變量Environment已經創建后,并且為環境變量Environment配置了命令行和Servlet等類型的環境變量后,此時會發布ApplicationEnvironmentPreparedEvent事件。

監聽ApplicationEnvironmentPreparedEvent事件的第一個監聽器是ConfigFileApplicationListener,因為是ConfigFileApplicationListener監聽器還要為環境變量Environment增加application.properties配置文件中的環境變量;此后還有一些也是監聽ApplicationEnvironmentPreparedEvent事件的其他監聽器監聽到此事件時,此時可以說環境變量Environment幾乎已經完全準備好了。

思考: 監聽同一事件的監聽器們執行監聽邏輯時是有順序的,我們可以想一下這個排序邏輯是什么時候排序的?還有為什么要這樣排序呢?

3.4.3 ApplicationContextInitializedEvent

// ApplicationContextInitializedEvent.java

public class ApplicationContextInitializedEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;
    /**
     * Create a new {@link ApplicationContextInitializedEvent} instance.
     * @param application the current application
     * @param args the arguments the application is running with
     * @param context the context that has been initialized
     */
    public ApplicationContextInitializedEvent(SpringApplication application,
            String[] args, ConfigurableApplicationContext context) {
        super(application, args);
        this.context = context;
    }
    /**
     * Return the application context.
     * @return the context
     */
    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
}

可以看到ApplicationContextInitializedEvent事件多了個ConfigurableApplicationContext類型的context屬性,context屬性的作用同樣是為了相應監聽器可以拿到這個context屬性執行一些邏輯,具體作用將在3.4.4詳述。

ApplicationContextInitializedEvent事件在ApplicationContext容器創建后,且為ApplicationContext容器設置了environment變量和執行了ApplicationContextInitializers的初始化方法后但在bean定義加載前觸發,標志ApplicationContext已經初始化完畢。

擴展: 可以看到ApplicationContextInitializedEvent是在為context容器配置environment變量后觸發,此時ApplicationContextInitializedEvent等事件只要有context容器的話,那么其他需要environment環境變量的監聽器只需要從context中取出environment變量即可,從而ApplicationContextInitializedEvent等事件沒必要再配置environment屬性。

3.4.4 ApplicationPreparedEvent

// ApplicationPreparedEvent.java

public class ApplicationPreparedEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;
    /**
     * Create a new {@link ApplicationPreparedEvent} instance.
     * @param application the current application
     * @param args the arguments the application is running with
     * @param context the ApplicationContext about to be refreshed
     */
    public ApplicationPreparedEvent(SpringApplication application, String[] args,
            ConfigurableApplicationContext context) {
        super(application, args);
        this.context = context;
    }
    /**
     * Return the application context.
     * @return the context
     */
    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
}

同樣可以看到ApplicationPreparedEvent事件多了個ConfigurableApplicationContext類型的context屬性,多了context屬性的作用是能讓監聽該事件的監聽器們能拿到context屬性,監聽器拿到context屬性一般有如下作用:

  1. 從事件中取出context屬性,然后可以增加一些后置處理器,比如ConfigFileApplicationListener監聽器監聽到ApplicationPreparedEvent事件后,然后取出context變量,通過context變量增加了PropertySourceOrderingPostProcessor這個后置處理器;
  2. 通過context屬性取出beanFactory容器,然后注冊一些bean,比如LoggingApplicationListener監聽器通過ApplicationPreparedEvent事件的context屬性取出beanFactory容器,然后注冊了springBootLoggingSystem這個單例bean;
  3. 通過context屬性取出Environment環境變量,然后就可以操作環境變量,比如PropertiesMigrationListener。

ApplicationPreparedEvent事件在ApplicationContext容器已經完全準備好時但在容器刷新前觸發,在這個階段bean定義已經加載完畢還有environment已經準備好可以用了。

3.4.5 ApplicationStartedEvent

// ApplicationStartedEvent.java

public class ApplicationStartedEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;
    /**
     * Create a new {@link ApplicationStartedEvent} instance.
     * @param application the current application
     * @param args the arguments the application is running with
     * @param context the context that was being created
     */
    public ApplicationStartedEvent(SpringApplication application, String[] args,
            ConfigurableApplicationContext context) {
        super(application, args);
        this.context = context;
    }
    /**
     * Return the application context.
     * @return the context
     */
    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
}

ApplicationStartedEvent事件將在容器刷新后但ApplicationRunnerCommandLineRunnerrun方法執行前觸發,標志Spring容器已經刷新,此時容器已經準備完畢了。

擴展: 這里提到了ApplicationRunnerCommandLineRunner接口有啥作用呢?我們一般會在Spring容器刷新完畢后,此時可能有一些系統參數等靜態數據需要加載,此時我們就可以實現了ApplicationRunnerCommandLineRunner接口來實現靜態數據的加載。

3.4.6 ApplicationReadyEvent

// ApplicationReadyEvent.java

public class ApplicationReadyEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;
    /**
     * Create a new {@link ApplicationReadyEvent} instance.
     * @param application the current application
     * @param args the arguments the application is running with
     * @param context the context that was being created
     */
    public ApplicationReadyEvent(SpringApplication application, String[] args,
            ConfigurableApplicationContext context) {
        super(application, args);
        this.context = context;
    }
    /**
     * Return the application context.
     * @return the context
     */
    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
}

ApplicationReadyEvent事件在調用完ApplicationRunnerCommandLineRunnerrun方法后觸發,此時標志SpringApplication已經正在運行。

3.4.7 ApplicationFailedEvent

// ApplicationFailedEvent.java

public class ApplicationFailedEvent extends SpringApplicationEvent {
    private final ConfigurableApplicationContext context;
    private final Throwable exception;
    /**
     * Create a new {@link ApplicationFailedEvent} instance.
     * @param application the current application
     * @param args the arguments the application was running with
     * @param context the context that was being created (maybe null)
     * @param exception the exception that caused the error
     */
    public ApplicationFailedEvent(SpringApplication application, String[] args,
            ConfigurableApplicationContext context, Throwable exception) {
        super(application, args);
        this.context = context;
        this.exception = exception;
    }
    /**
     * Return the application context.
     * @return the context
     */
    public ConfigurableApplicationContext getApplicationContext() {
        return this.context;
    }
    /**
     * Return the exception that caused the failure.
     * @return the exception
     */
    public Throwable getException() {
        return this.exception;
    }
}

可以看到ApplicationFailedEvent事件除了多了一個context屬性外,還多了一個Throwable類型的exception屬性用來記錄SpringBoot啟動失敗時的異常。

ApplicationFailedEvent事件在SpringBoot啟動失敗時觸發,標志SpringBoot啟動失敗。

4 小結

此篇文章相對簡單,對SpringBoot內置的7種生命周期事件進行了詳細分析。我們還是引用上篇文章的一張圖來回顧一下這些生命周期事件及其用途:

5 寫在最后

由于有一些小伙伴們建議之前有些源碼分析文章太長,導致耐心不夠,看不下去,因此,之后的源碼分析文章如果太長的話,筆者將會考慮拆分為幾篇文章,這樣就比較短小了,比較容易看完,嘿嘿。

【源碼筆記】Github地址:

https://github.com/yuanmabiji/Java-SourceCode-Blogs

點贊搞起來,嘿嘿嘿!


公眾號【源碼筆記】專注于Java后端系列框架的源碼分析。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,665評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,627評論 2 380