通常,我們只需為一個類添加@SpringBootApplication注解,然后再添加一個main方法,其內(nèi)固定的寫法為SpringApplication.run(Application.class, args)。由此,便可啟動Spring Boot服務(wù)。
具體而言,Spring Boot的啟動流程包括以下幾個步驟:
載入 Spring Boot 應(yīng)用的啟動類
根據(jù)啟動類所在的包路徑掃描相關(guān)的類文件
基于掃描到的類自動配置 Spring 應(yīng)用
激活內(nèi)嵌的 Web 服務(wù)器
啟動 Spring 應(yīng)用程序的運(yùn)行
深入解析Spring Boot啟動過程的源碼實(shí)現(xiàn):
@SpringBootConfiguration // 標(biāo)記該類為 Spring Boot 應(yīng)用程序的配置類
@EnableAutoConfiguration // 啟用 Spring Boot 的自動配置機(jī)制
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args); // 執(zhí)行 Spring Boot 應(yīng)用程序
}
}
在啟動 Spring Boot 應(yīng)用程序時,需要在啟動類上使用 @SpringBootApplication 注解,以通知 Spring Boot 這是應(yīng)用程序的啟動類。@SpringBootApplication 注解包含了三個子注解,分別為 @Configuration、@EnableAutoConfiguration 和 @ComponentScan,分別表示該類是配置類、啟用自動配置和掃描組件。
在 main 方法中,我們可以使用以下代碼來啟動 Spring Boot 應(yīng)用程序:
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
該方法接受兩個參數(shù),第一個參數(shù)是啟動類的類對象 MyApp.class,第二個參數(shù)是主方法的參數(shù) args,主方法的參數(shù)可以通過命令行參數(shù)傳遞給應(yīng)用程序。
在 SpringApplication.run 方法中,會執(zhí)行以下幾個步驟:
實(shí)例化一個 SpringApplication 對象,它存儲了 Spring Boot 應(yīng)用程序的所有配置信息。
基于 SpringApplication 中的配置信息,初始化一個 ApplicationContext 對象,它是 Spring 應(yīng)用程序的上下文。
在 ApplicationContext 中注冊所有帶有 @Configuration 注解的類。
根據(jù) @EnableAutoConfiguration 注解,自動配置 Spring 應(yīng)用程序。
掃描所有帶有 @Component 注解的類,并將它們注冊到 ApplicationContext 中。
啟動嵌入式 Web 服務(wù)器。
運(yùn)行 Spring 應(yīng)用程序。
以下是 SpringApplication.run 方法的源代碼注釋:文章來源:http://www.zghlxwxcb.cn/news/detail-453504.html
/**
* 這是 Spring Boot 應(yīng)用程序的入口點(diǎn)
* @param primarySource 應(yīng)用程序的主要源(通常是一個@Configuration類)。
* @param args 命令行參數(shù)。
* @return ApplicationContext。
*/
public static ConfigurableApplicationContext run(Class\<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
/**
* 這是 Spring Boot 應(yīng)用程序的入口點(diǎn)。
* @param primarySources 應(yīng)用程序的主要源(通常是一個@Configuration類)。
* @param args 命令行參數(shù)。
* @return ApplicationContext。
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 創(chuàng)建一個新的 SpringApplication 實(shí)例,主要用于設(shè)置 SpringApplication 的屬性
SpringApplication application = new SpringApplication(primarySources);
// 應(yīng)用程序啟動時應(yīng)用的環(huán)境。 這可以被用來定制化應(yīng)用程序環(huán)境以及激活不同的配置文件屬性。默認(rèn)情況下, SpringApplication 將使用適當(dāng)?shù)?PropertySourceLoader 從 application.properties 中加載默認(rèn)的屬性。
applyInitializers(application);
// 設(shè)置命令行參數(shù)
ConfigurableApplicationContext context = application.run(args);
// 后置操作
postProcessApplicationContext(context);
return context;
}
public static ConfigurableApplicationContext launch(Class<?> primarySource, String... args) {
return launch(new Class<?>[] { primarySource }, args);
}
public static ConfigurableApplicationContext launch(Class<?>[] primarySources, String[] args) {
// 創(chuàng)建 Launcher 對象,包含了所有的應(yīng)用程序配置信息
Launcher launcher = new Launcher(primarySources);
// 運(yùn)行應(yīng)用程序,并返回上下文對象
return launcher.run(args);
}
public ConfigurableApplicationContext run(String... args) {
// 創(chuàng)建并啟動 ConfigurableApplicationContext 對象,返回該對象
ConfigurableApplicationContext context = createApplicationContext();
// 執(zhí)行應(yīng)用程序的監(jiān)聽器
listeners.starting(this.applicationArguments);
try {
// 準(zhǔn)備 ApplicationContext 環(huán)境
prepareEnvironment(context, this.environment);
// 配置 ApplicationContext
configureIgnoreBeanInfo(context);
// 執(zhí)行所有的 ApplicationContextInitializer
applyInitializers(context);
// 執(zhí)行所有的 LauncherRunListener 的 starting 方法
listeners.contextPrepared(context);
// 打印應(yīng)用程序的 Banner
Banner printedBanner = printBanner();
// 創(chuàng)建 ApplicationContext
context.refresh();
// 將 ApplicationContext 注冊到 JVM 關(guān)閉鉤子中
prepareContext(context, printedBanner);
// 執(zhí)行所有的 ApplicationContextInitializer 的 postProcessApplicationContext 方法
postProcessApplicationContext(context);
// 執(zhí)行所有的 LauncherRunListener 的 contextLoaded 方法
listeners.contextLoaded(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 執(zhí)行所有的 LauncherRunListener 的 started 方法
listeners.started(context);
// 啟動嵌入式的 Web 服務(wù)器
callRunners(context, this.applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 執(zhí)行所有的 LauncherRunListener 的 running 方法
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
return context;
}
在 SpringApplication.run 方法的執(zhí)行過程中,會涉及到一系列關(guān)鍵步驟,其中包括 prepareEnvironment、applyInitializers、postProcessApplicationContext和callRunners 等方法的調(diào)用。這些方法都扮演著啟動 Spring Boot 應(yīng)用程序的重要角色,它們會對應(yīng)用程序進(jìn)行配置、初始化、啟動等操作,確保整個應(yīng)用程序的正常運(yùn)行和穩(wěn)定性。
好的,基本上三分鐘已經(jīng)講完了,希望對各位有所幫助。如果還有任何疑問或需要補(bǔ)充的內(nèi)容,歡迎在評論區(qū)或私信留言。今天的總結(jié)內(nèi)容就到這里了,感謝大家的閱讀。文章來源地址http://www.zghlxwxcb.cn/news/detail-453504.html
到了這里,關(guān)于還不懂 Spring Boot 啟動流程的,看這一篇就夠了!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!