可能是全網最全的SpringBoot啓動流程源碼分析(基於 2.1.5 版本)

使用 Spring Boot 啓動一個微服務十分簡單,只需要在啓動類上調用 SpringApplication 的run方法即可

點擊進入run方法

1 run

靜態輔助類,可用於運行使用默認配置(即我們添加的一系列註解)的指定源的 SpringApplication 。

  • primarySource - 要載入的主要源,即指定源,這裏爲傳入的Application.class
    Class<?> :泛型決定了任何類都可以傳入
  • args - 應用程序參數(通常是從main方法傳遞)
  • 返回:正在運行的ApplicationContext

上面是 SpringApplication實例對象構造方法初始化過程
繼續看這個 SpringApplication 對象的 run 方法的源碼和運行流程

public ConfigurableApplicationContext run(String... args) {    
   
    // 1 創建並啓動計時監控類
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();    

    // 2 初始化應用上下文和異常報告集合
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();    

    // 3 設置系統屬性 java.awt.headless 的值,默認爲true
    configureHeadlessProperty();    

    // 4、創建所有 Spring 運行監聽器併發布應用啓動事件
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();    
    try {        // 5、初始化默認應用參數類
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                args);                
        // 6、根據運行監聽器和應用參數來準備 Spring 環境
        ConfigurableEnvironment environment = prepareEnvironment(listeners,
                applicationArguments);
        configureIgnoreBeanInfo(environment);        
        // 7、創建 Banner 打印類
        Banner printedBanner = printBanner(environment);        
        // 8、創建應用上下文
        context = createApplicationContext();        
        // 9、準備異常報告器
        exceptionReporters = getSpringFactoriesInstances(
                SpringBootExceptionReporter.class,                new Class[] { ConfigurableApplicationContext.class }, context);                
        // 10、準備應用上下文
        prepareContext(context, environment, listeners, applicationArguments,
                printedBanner);                
        // 11、刷新應用上下文
        refreshContext(context);        
        // 12、應用上下文刷新後置處理
        afterRefresh(context, applicationArguments);        
        // 13、停止計時監控類
        stopWatch.stop();        
        // 14、輸出日誌記錄執行主類名、時間信息
        if (this.logStartupInfo) {            new StartupInfoLogger(this.mainApplicationClass)
                    .logStarted(getApplicationLog(), stopWatch);
        }        
        // 15、發佈應用上下文啓動完成事件
        listeners.started(context);        
        // 16、執行所有 Runner 運行器
        callRunners(context, applicationArguments);
    }    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);        throw new IllegalStateException(ex);
    }    try {        // 17、發佈應用上下文就緒事件
        listeners.running(context);
    }    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);        throw new IllegalStateException(ex);
    }    
    // 18、返回應用上下文
    return context;
}

1 創建並啓動計時監控類

  • 類概述
  • 首先記錄了當前任務的名稱,默認爲空串,然後記錄當前 Spring Boot 應用啓動的開始時間
    StopWatch#

設置系統屬性 java.awt.headless 的值


SpringApplication#

對於一個 Java 服務器來說經常要處理一些圖形元素,例如地圖的創建或者圖形和圖表等。這些API基本上總是需要運行一個X-server以便能使用AWT(Abstract Window Toolkit,抽象窗口工具集)。然而運行一個不必要的 X-server 並不是一種好的管理方式。有時你甚至不能運行 X-server,因此最好的方案是運行 headless 服務器,來進行簡單的圖像處理。

參考:www.cnblogs.com/princessd8251/p/4000016.html

創建所有 Spring 運行監聽器併發布應用啓動事件

調用getSpringFactoriesInstances 來獲取配置的監聽器名稱並實例化所有的類

SpringApplicationRunListener所有監聽器配置在spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories這個配置文件裏面。



初始化默認應用參數類



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章