SpringBoot啟動(dòng)時(shí)執(zhí)行自定義內(nèi)容的五種方法
引言
在Spring Boot應(yīng)用開(kāi)發(fā)中,我們經(jīng)常需要在應(yīng)用啟動(dòng)時(shí)執(zhí)行一些初始化邏輯,比如加載配置、初始化數(shù)據(jù)、啟動(dòng)后臺(tái)任務(wù)等。Spring Boot提供了多種方式來(lái)實(shí)現(xiàn)這一需求,本文將詳細(xì)介紹5種常用的方法,并分析它們的適用場(chǎng)景和優(yōu)缺點(diǎn)。
1. 使用CommandLineRunner或ApplicationRunner接口(推薦)
Spring Boot專門提供了CommandLineRunner和ApplicationRunner接口,用于在應(yīng)用啟動(dòng)后執(zhí)行自定義邏輯。它們的區(qū)別在于參數(shù)傳遞方式不同:
1.1 CommandLineRunner
CommandLineRunner接收一個(gè)字符串?dāng)?shù)組(String... args),適合處理簡(jiǎn)單的命令行參數(shù)。
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("應(yīng)用啟動(dòng)后執(zhí)行 - CommandLineRunner");
// 可以在這里訪問(wèn)Spring容器中的Bean
}
}
1.2 ApplicationRunner
ApplicationRunner接收ApplicationArguments對(duì)象,提供了更結(jié)構(gòu)化的參數(shù)訪問(wèn)方式(如getOptionNames()、getNonOptionArgs()等)。
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("應(yīng)用啟動(dòng)后執(zhí)行 - ApplicationRunner");
// 可以訪問(wèn)命令行參數(shù)
if (args.containsOption("debug")) {
System.out.println("Debug模式已啟用");
}
}
}
1.3 控制執(zhí)行順序
如果有多個(gè)Runner,可以使用@Order注解或?qū)崿F(xiàn)Ordered接口來(lái)控制執(zhí)行順序:
@Component
@Order(1) // 數(shù)字越小,優(yōu)先級(jí)越高
public class FirstRunner implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("第一個(gè)Runner執(zhí)行");
}
}
@Component
@Order(2)
public class SecondRunner implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("第二個(gè)Runner執(zhí)行");
}
}
適用場(chǎng)景:
- 需要在應(yīng)用完全啟動(dòng)后執(zhí)行初始化邏輯(如加載數(shù)據(jù)、啟動(dòng)后臺(tái)任務(wù))。
- 需要訪問(wèn)Spring容器中的Bean。
優(yōu)點(diǎn):
- 官方推薦,專為啟動(dòng)后執(zhí)行設(shè)計(jì)。
- 支持執(zhí)行順序控制。
缺點(diǎn):
- 無(wú)法在Bean初始化階段執(zhí)行。
2. 使用@PostConstruct注解
@PostConstruct是JSR-250標(biāo)準(zhǔn)注解,用于標(biāo)記在Bean初始化完成后執(zhí)行的方法。
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
@Component
public class MyPostConstructBean {
@PostConstruct
public void init() {
System.out.println("Bean初始化后執(zhí)行 - @PostConstruct");
// 可以在這里執(zhí)行初始化邏輯
}
}
適用場(chǎng)景:
- 需要在單個(gè)Bean初始化完成后執(zhí)行邏輯(如數(shù)據(jù)庫(kù)連接測(cè)試、緩存預(yù)熱)。
優(yōu)點(diǎn):
- 簡(jiǎn)單易用,適用于單個(gè)Bean的初始化。
缺點(diǎn):
- 無(wú)法控制多個(gè)Bean的執(zhí)行順序。
- 執(zhí)行時(shí)機(jī)較早(在Bean初始化后,但應(yīng)用可能還未完全啟動(dòng))。
3. 實(shí)現(xiàn)ApplicationListener監(jiān)聽(tīng)啟動(dòng)事件
Spring Boot提供了多種事件,如ApplicationReadyEvent(應(yīng)用完全啟動(dòng)后觸發(fā))、ApplicationStartedEvent(應(yīng)用啟動(dòng)后但未處理請(qǐng)求時(shí)觸發(fā))等。
3.1 監(jiān)聽(tīng)ApplicationReadyEvent
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
System.out.println("應(yīng)用完全啟動(dòng)后執(zhí)行 - ApplicationReadyEvent");
// 可以在這里執(zhí)行應(yīng)用啟動(dòng)后的邏輯
}
}
適用場(chǎng)景:
- 需要在應(yīng)用完全啟動(dòng)后執(zhí)行邏輯(如發(fā)送啟動(dòng)完成通知)。
優(yōu)點(diǎn):
- 精確控制執(zhí)行時(shí)機(jī)(應(yīng)用完全啟動(dòng)后)。
缺點(diǎn):
- 代碼稍顯冗長(zhǎng),不如
CommandLineRunner簡(jiǎn)潔。
4. 使用InitializingBean接口
InitializingBean是Spring提供的接口,其afterPropertiesSet()方法會(huì)在Bean屬性設(shè)置完成后執(zhí)行。
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class MyInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean屬性設(shè)置后執(zhí)行 - InitializingBean");
// 可以在這里執(zhí)行初始化邏輯
}
}
適用場(chǎng)景:
- 需要在Bean屬性設(shè)置完成后執(zhí)行邏輯(如校驗(yàn)配置)。
優(yōu)點(diǎn):
- 適用于單個(gè)Bean的初始化。
缺點(diǎn):
- 執(zhí)行時(shí)機(jī)較早(在Bean初始化階段)。
- 不如
@PostConstruct簡(jiǎn)潔。
5. 在main方法中直接執(zhí)行
如果邏輯非常簡(jiǎn)單,可以直接在main方法中執(zhí)行:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
System.out.println("Spring Boot啟動(dòng)前執(zhí)行");
SpringApplication.run(MyApplication.class, args);
System.out.println("Spring Boot啟動(dòng)過(guò)程完成后執(zhí)行");
// 注意:這里的代碼會(huì)在Spring Boot啟動(dòng)后立即執(zhí)行,但不一定是所有Bean都初始化完成
}
}
適用場(chǎng)景:
- 極簡(jiǎn)單的初始化邏輯(如打印日志)。
優(yōu)點(diǎn):
- 無(wú)需額外代碼。
缺點(diǎn):
- 無(wú)法保證所有Bean都已初始化。
- 不推薦用于復(fù)雜邏輯。
總結(jié)
| 方法 | 適用場(chǎng)景 | 執(zhí)行時(shí)機(jī) | 優(yōu)點(diǎn) | 缺點(diǎn) |
|---|---|---|---|---|
| CommandLineRunner/ApplicationRunner | 應(yīng)用啟動(dòng)后執(zhí)行初始化邏輯 | 應(yīng)用完全啟動(dòng)后 | 官方推薦,支持順序控制 | 無(wú)法在Bean初始化階段執(zhí)行 |
| @PostConstruct | 單個(gè)Bean初始化后執(zhí)行邏輯 | Bean初始化后 | 簡(jiǎn)單易用 | 無(wú)法控制多個(gè)Bean的執(zhí)行順序 |
| ApplicationListener | 應(yīng)用完全啟動(dòng)后執(zhí)行邏輯 | 應(yīng)用完全啟動(dòng)后 | 精確控制執(zhí)行時(shí)機(jī) | 代碼稍顯冗長(zhǎng) |
| InitializingBean | 單個(gè)Bean屬性設(shè)置后執(zhí)行邏輯 | Bean屬性設(shè)置后 | 適用于單個(gè)Bean | 執(zhí)行時(shí)機(jī)較早 |
| main方法直接執(zhí)行 | 極簡(jiǎn)單的初始化邏輯 | Spring Boot啟動(dòng)后 | 無(wú)需額外代碼 | 無(wú)法保證所有Bean初始化 |
推薦選擇:
- 需要應(yīng)用完全啟動(dòng)后執(zhí)行邏輯 → 使用CommandLineRunner或ApplicationRunner(推薦)。
- 需要在單個(gè)Bean初始化后執(zhí)行邏輯 → 使用@PostConstruct。
- 需要精確控制執(zhí)行時(shí)機(jī) → 使用ApplicationListener監(jiān)聽(tīng)ApplicationReadyEvent。
希望本文能幫助你選擇最適合的方式,在Spring Boot啟動(dòng)時(shí)執(zhí)行自定義邏輯!
以上就是SpringBoot啟動(dòng)時(shí)執(zhí)行自定義內(nèi)容的五種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot執(zhí)行自定義內(nèi)容的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PowerJob的AbstractScriptProcessor實(shí)現(xiàn)類工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的AbstractScriptProcessor源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
1小時(shí)快速上手RabbitMQ(簡(jiǎn)介及安裝過(guò)程)
RabbitMQ簡(jiǎn)稱MQ全稱是Message Queue(消息隊(duì)列),是在消息的傳輸過(guò)程中保存消息的容器,多用于分布式系統(tǒng)之間進(jìn)行通信,本文給大家講解了RabbitMQ簡(jiǎn)介與安裝,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友跟隨小編一起看看吧2023-01-01
springboot整合webservice使用簡(jiǎn)單案例總結(jié)
WebService是一個(gè)SOA(面向服務(wù)的編程)的架構(gòu),它是不依賴于語(yǔ)言,平臺(tái)等,可以實(shí)現(xiàn)不同的語(yǔ)言間的相互調(diào)用,下面這篇文章主要給大家介紹了關(guān)于springboot整合webservice使用的相關(guān)資料,需要的朋友可以參考下2024-07-07
詳解Springboot-MyBatis配置-配置端口號(hào)與服務(wù)路徑(idea社區(qū)版2023.1.4+apache-mav
這篇文章主要介紹了Springboot-MyBatis配置-配置端口號(hào)與服務(wù)路徑(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Springboot使用ResponseBody漢字返回問(wèn)號(hào)問(wèn)題
這篇文章主要介紹了Springboot使用ResponseBody漢字返回問(wèn)號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
探討如何在Eclipse中過(guò)濾版本控制文件.svn
本篇文章是對(duì)在Eclipse中過(guò)濾版本控制文件.svn的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù)
本篇文章主要介紹了spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
java實(shí)現(xiàn)航班信息查詢管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)航班信息查詢管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

