一文帶你了解SpringBoot的啟動原理
Spring Boot 的啟動原理可以概括為以下幾個步驟:
- 加載 Spring Boot 應用程序的啟動類
- 根據(jù)啟動類所在的包路徑掃描相關的類
- 根據(jù)掃描到的類自動配置 Spring 應用程序
- 啟動嵌入式的 Web 服務器
- 運行 Spring 應用程序
Spring Boot 啟動過程的源碼剖析:
@SpringBootApplication // 標記該類為 Spring Boot 應用程序的啟動類 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); // 執(zhí)行 Spring Boot 應用程序 } }
在 Spring Boot 應用程序的啟動類中,需要使用 @SpringBootApplication 注解標記該類,以告訴 Spring Boot 這是應用程序的啟動類。@SpringBootApplication 注解包含了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 這三個注解,分別表示該類是配置類、開啟自動配置和掃描組件。
在 main
方法中,調(diào)用 SpringApplication.run
方法來啟動 Spring Boot 應用程序。該方法接受兩個參數(shù),第一個參數(shù)是啟動類的類對象,第二個參數(shù)是主方法的參數(shù)。在 SpringApplication.run
方法中,會執(zhí)行以下幾個步驟:
- 創(chuàng)建一個 SpringApplication 對象,該對象包含了所有的 Spring Boot 應用程序配置信息。
- 根據(jù) SpringApplication 對象中的配置信息,創(chuàng)建一個 ApplicationContext 對象,該對象是 Spring 應用程序的上下文。
- 將所有的 @Configuration 類型的類注冊到 ApplicationContext 對象中。
- 根據(jù) @EnableAutoConfiguration 注解,自動配置 Spring 應用程序。
- 掃描所有的 @Component 注解的類,并將其注冊到 ApplicationContext 中。
- 啟動嵌入式的 Web 服務器。
- 運行 Spring 應用程序。
下面是 SpringApplication.run
方法的源碼注釋:
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); } public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { // 創(chuàng)建 SpringApplication 對象,包含了所有的 Spring Boot 應用程序配置信息 SpringApplication application = new SpringApplication(primarySources); // 運行 Spring Boot 應用程序,并返回上下文對象 return application.run(args); } public ConfigurableApplicationContext run(String... args) { // 創(chuàng)建并啟動 ConfigurableApplicationContext 對象,返回該對象 ConfigurableApplicationContext context = createApplicationContext(); // 執(zhí)行 Spring 應用程序的監(jiān)聽器 listeners.starting(this.applicationArguments); try { // 準備 ApplicationContext 環(huán)境 prepareEnvironment(context, this.environment); // 配置 ApplicationContext configureIgnoreBeanInfo(context); // 執(zhí)行所有的 ApplicationContextInitializer applyInitializers(context); // 執(zhí)行所有的 SpringApplicationRunListener 的 starting 方法 listeners.contextPrepared(context); // 打印 Spring Boot 應用程序的 Banner Banner printedBanner = printBanner(); // 創(chuàng)建 ApplicationContext context.refresh(); // 將 ApplicationContext 注冊到 JVM 關閉鉤子中 prepareContext(context, printedBanner); // 執(zhí)行所有的 ApplicationContextInitializer 的 postProcessApplicationContext 方法 postProcessApplicationContext(context); // 執(zhí)行所有的 SpringApplicationRunListener 的 contextLoaded 方法 listeners.contextLoaded(context); } catch (Throwable ex) { handleRunFailure(context, ex, listeners); throw new IllegalStateException(ex); } try { // 執(zhí)行所有的 SpringApplicationRunListener 的 started 方法 listeners.started(context); // 啟動嵌入式的 Web 服務器 callRunners(context, this.applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, listeners); throw new IllegalStateException(ex); } try { // 執(zhí)行所有的 SpringApplicationRunListener 的 running 方法 listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, listeners); throw new IllegalStateException(ex); } return context; }
在 SpringApplication.run 方法中,還會調(diào)用一些其他的方法,如 prepareEnvironment、applyInitializers、postProcessApplicationContext、callRunners 等,這些方法是 Spring Boot 應用程序啟動的關鍵步驟,它們會對應用程序進行配置、初始化、啟動等操作。
以上就是一文帶你了解SpringBoot的啟動原理的詳細內(nèi)容,更多關于SpringBoot啟動原理的資料請關注腳本之家其它相關文章!
相關文章
Java業(yè)務中臺確保數(shù)據(jù)一致性的解決方案
數(shù)據(jù)一致性通常指關聯(lián)數(shù)據(jù)之間的邏輯關系是否正確和完整。而數(shù)據(jù)存儲的一致性模型則可以認為是存儲系統(tǒng)和數(shù)據(jù)使用者之間的一種約定。如果使用者遵循這種約定,則可以得到系統(tǒng)所承諾的訪問結(jié)果2021-10-10Spring Boot配置接口WebMvcConfigurer的實現(xiàn)
這篇文章主要介紹了SpringBoot配置接口WebMvcConfigurer的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11解決Maven 項目報錯 java.httpservlet和synchronized使用方法
下面小編就為大家?guī)硪黄鉀QMaven 項目報錯 java.httpservlet和synchronized使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07關于@Autowierd && @Resource 你真的了解嗎
這篇文章主要介紹了關于@Autowierd && @Resource的具體使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java數(shù)據(jù)結(jié)構之AC自動機算法的實現(xiàn)
AC自動機算法常被認為是Trie樹+KMP算法的結(jié)合體,它是一個多模式匹配算法,在模式匹配領域被廣泛應用。本文將詳細為大家介紹AC自動機的原理與實現(xiàn)方法,感興趣的可以了解一下2022-12-12