Spring源碼0. springboot啟動(dòng)流程以及源碼剖析目錄

目錄

????1. 初始化SpringApplication實(shí)例

????2. 發(fā)布啟動(dòng)事件ApplicationStartingEvent

????3. 封裝命令行參數(shù)DefaultApplicationArguments

????4. prepareEnvironment()準(zhǔn)備環(huán)境

????5. printBanner()打印Banner

????6. createApplicationContext()創(chuàng)建應(yīng)用上下文

????7. SpringBootExceptionReporter異常上報(bào)

????8. prepareContext()準(zhǔn)備應(yīng)用上下文

????9. refreshContext()刷新應(yīng)用上下文

????10. 啟動(dòng)完成, 發(fā)布ApplicationStartedEvent, ApplicationReadyEvent事件


前言

當(dāng)我們使用springboot的時(shí)候, 我們只需要配置如下啟動(dòng)類, spring便可以配置所有的環(huán)境和上下文, 本文基于springboot2.1.3, 剖析spring的啟動(dòng)流程和原理

@SpringBootApplication
public class YanggxApplication {
    public static void main(String[] args) {
        //調(diào)用SpringApplication的run方法
        SpringApplication.run(YanggxApplication.class, args);
    }
}

SpringApplication運(yùn)行代碼

SpringApplication整個(gè)啟動(dòng)流程大概分為10個(gè)重要的步驟, 將會(huì)在之后的文章中逐個(gè)步驟分析

public class SpringApplication {
    
    //SpringApplication.run方法
    public static ConfigurableApplicationContext run(Class<?>[] primarySources,
            String[] args) {
        //步驟1. 初始化SpringApplication實(shí)例
        return new SpringApplication(primarySources).run(args);
    }

    /**
     * 啟動(dòng)Spring 應(yīng)用
     * @param args YanggxApplication中main函數(shù)的參數(shù)
     */
    public ConfigurableApplicationContext run(String... args) {
        //實(shí)例化一個(gè)StopWatch實(shí)例, 監(jiān)控項(xiàng)目運(yùn)行時(shí)間
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //初始化Spring上下文和錯(cuò)誤報(bào)告參數(shù)
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        //配置headless,在沒有顯示器,鼠標(biāo),鍵盤的情況下,仍然可以調(diào)用顯示,輸入輸出的方法
        configureHeadlessProperty();
    
        //步驟2.發(fā)布Spring啟動(dòng)事件
        //獲取SpringApplicationRunListeners
        //listeners的監(jiān)聽器列表中只包含了一個(gè)EventPublishingRunListener對(duì)象
        SpringApplicationRunListeners listeners = getRunListeners(args);
    
        //調(diào)用SpringFactoriesLoader#starting方法,
        //最終調(diào)用EventPublishingRunListener#starting方法
        listeners.starting();
        
        try {
            //步驟3. 封裝命令行參數(shù)DefaultApplicationArguments
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            
            //步驟4. prepareEnvironment()準(zhǔn)備環(huán)境
            ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
            configureIgnoreBeanInfo(environment);
            
            //步驟5: printBanner()打印Banner
            Banner printedBanner = printBanner(environment);
            
            //步驟6: createApplicationContext()創(chuàng)建應(yīng)用上下文
            context = createApplicationContext();
            
            //步驟7: SpringBootExceptionReporter異常上報(bào)
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            //步驟8: prepareContext()準(zhǔn)備應(yīng)用上下文
            prepareContext(context, environment, listeners, applicationArguments,printedBanner);
            
            //步驟9: refreshContext()刷新應(yīng)用上下文
            refreshContext(context);
            
            //步驟10: 啟動(dòng)完成, 發(fā)布ApplicationStartedEvent, ApplicationReadyEvent事件
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(),stopWatch);
            }
            //發(fā)布ApplicationStartedEvent
            listeners.started(context);
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }
    
        try {
            //發(fā)布ApplicationReadyEvent事件
            listeners.running(context);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容