spring-boot 運(yùn)行流程粗略紀(jì)要

springboot運(yùn)行流程,主要集中在springApplication.class這個(gè)類中,我們可以通過springboot啟動(dòng)類調(diào)用的springApplication.run()方法為起點(diǎn),粗略的閱讀源碼,下文為流程粗略紀(jì)要,讀者可以根據(jù)紀(jì)要閱讀一遍。

一、new SpringApplication,執(zhí)行initialize()方法
    1)、判斷是否是web環(huán)境
    2)、加載所有classpath目錄下META-INF/spring.factories配置文件中的ApplicationContextInitializer
    3)、加載所有classpath目錄下META-INF/spring.factories配置文件中的ApplicationListener
    4)、推斷main方法所在的類
二、執(zhí)行run方法
    1)設(shè)置java.awt.headless系統(tǒng)變量
    2)加載所有classpath目錄下META-INF/spring.factories配置文件中的SpringApplicationRunListener
    3)執(zhí)行所有SpringApplicationRunListener的started方法
    4)實(shí)例化ApplicationArguments對(duì)象
    5)創(chuàng)建environment
    6)配置environment,主要是把run方法配置到environment
    7)執(zhí)行所有SpringApplicationRunListener的environmentPrepared方法
    8)如果不是webEnvironment,且有必要,則把當(dāng)前Environment轉(zhuǎn)換為標(biāo)準(zhǔn)的Environment
    9)初始化ApplicationContext,如果是web Environment,則實(shí)例化org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
        否則實(shí)例化org.springframework.context.annotation.AnnotationConfigApplicationContext
    10)如果beanNameGenerator不為空,就把beanNameGenerator對(duì)象注入到context中
    11)回調(diào)所有的ApplicationContextInitializer
    12)執(zhí)行所有SpringApplicationRunListener的contextPrepared方法
    13)依次往spring容器中注入ApplicationArguments、Banner
    14)加載所有的源到context中(源即為springboot項(xiàng)目的啟動(dòng)類)
    15)執(zhí)行所有SpringApplicationRunListeners的contextLoaded方法
    16)執(zhí)行applicationContext的refresh方法,并且調(diào)用ApplicationContext.registerShutdownHook()方法
    17)回調(diào),獲取容器中所有的ApplicationRunner、CommandLineRunner對(duì)象,然后排序、依次調(diào)用
    18)執(zhí)行SpringApplicationRunListeners.finished方法,結(jié)束所有的監(jiān)聽
    19)結(jié)束啟動(dòng)計(jì)時(shí),如果啟動(dòng)狀態(tài)異常,打印日志

建議讀者先閱讀我的前幾篇Spring-boot事件監(jiān)聽和源碼分析spring-boot ApplicationContextInitializer實(shí)現(xiàn)與使用、spring-boot CommandLineRunner、ApplicationRunner實(shí)現(xiàn)與使用文章,閱讀后就能理解他們執(zhí)行的先后順序啦~

最后編輯于
?著作權(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)容