Spring Boot啟動流程斷點過程解析
這篇文章主要介紹了Spring Boot啟動流程斷點過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
啟動入口

跟進run方法 : 一個用來使用默認的配置從特定的源運行SpringApplication的靜態(tài)幫助類。
這個類有兩個重載方法,另一個用來傳入多個源。通常,單個參數(shù)方法是數(shù)組方法的一個特例

創(chuàng)建一個新的SpringApplication實例。這個應用程序上下文會從特定的源加載Beans,這個實例會在調(diào)用run方法之前被定制化。
Web應用程序類型的枚舉:WebApplicationType,包含NONE(不是web應用),SERVLET(基于Servlet的web應用),REACTIVE(基于Reactive的web應用)
- 直接jar包運行不使用web容器
- 使用嵌入式的Servlet web容器
- 使用反應式的web容器

setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class));
用于創(chuàng)建和加載Spring工廠方法實例




4.運行SpringApplication的run方法

Java SPI在 Spring Boot中的應用
SpringBoot底層的自動化都是由這些SPI實現(xiàn)類來實現(xiàn)的:初始化,監(jiān)聽器,自動配置導入監(jiān)聽器,自動配置導入過濾器,自動配置,失敗分析器,可用模板提供者

Spring Boot找到main方式的方式
通過拋異常的形式來獲取堆棧信息,再獲取啟動類的信息。

以上都是new SpringBootApplication的過程,下面分析run方法
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* 運行一個Spring應用,創(chuàng)建和刷新一個新的ApplicationContext
* @param args the application arguments (usually passed from a Java main method)
* 應用參數(shù)通過java main方法傳遞過來
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
// 任務計時器工具,可同時計數(shù)多個任務
StopWatch stopWatch = new StopWatch();
stopWatch.start();
//ApplicationContext是Spring的中心接口,為應用提供配置:1bean工廠2加載資源3注冊的監(jiān)聽器發(fā)布事件4解析消息
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//headless 模式:服務器端模式,表示系統(tǒng)沒有鍵盤鼠標等前端應用
configureHeadlessProperty();
//監(jiān)聽器容器,對run方法各個階段事件進行監(jiān)聽,觀察者模式
SpringApplicationRunListeners listeners = getRunListeners(args);
//監(jiān)聽相應的事件,SpringApplicationEvent下的一個實現(xiàn)
listeners.starting();
try {
//提供了對于運行SpringApplication參數(shù)的訪問
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//環(huán)境配置:是servlet,reactive或者java應用環(huán)境,觸發(fā)evn準備好的事件
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成redis與session實現(xiàn)分布式單點登錄
這篇文章主要介紹了SpringBoot集成redis與session實現(xiàn)分布式單點登錄,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
StringUtils,CollectionUtils判斷為空的方法和原生代碼哪個效率最高
這篇文章主要介紹了StringUtils,CollectionUtils判斷為空的方法和原生代碼哪個效率最高,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
java8 forEach結(jié)合Lambda表達式遍歷 List操作
這篇文章主要介紹了java8 forEach結(jié)合Lambda表達式遍歷 List操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
logback的UNDEFINED_PROPERTY屬性源碼執(zhí)行流程解讀
這篇文章主要為大家介紹了logback的UNDEFINED_PROPERTY屬性源碼執(zhí)行流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

