在SpringBoot啟動(dòng)時(shí)執(zhí)行特定代碼的常見方法小結(jié)
在SpringBoot的項(xiàng)目中,經(jīng)常會(huì)遇到需要在項(xiàng)目啟動(dòng)后執(zhí)行一些操作的情形,如加載配置,初始化數(shù)據(jù),緩存預(yù)熱等,本文整理了幾種常見的在項(xiàng)目啟動(dòng)時(shí)執(zhí)行特定代碼的方法。
1.使用 @PostConstruct 注解
@Slf4j
@Component
public class MyInit {
@PostConstruct
public void init() {
log.info("PostConstruct initialized~~~");
}
}
優(yōu)點(diǎn):
- 簡(jiǎn)單直接:只需在方法上添加 @PostConstruct 注解,方法會(huì)在Bean初始化后立即執(zhí)行。
- 靈活性高:可以在任何Bean中使用,適合對(duì)特定Bean進(jìn)行初始化邏輯。
缺點(diǎn):
- 無法控制執(zhí)行順序:多個(gè) @PostConstruct 方法的執(zhí)行順序不可控。
- 不適合復(fù)雜啟動(dòng)邏輯:在Bean初始化完成后執(zhí)行,但此時(shí)可能其他Bean尚未完全初始化完成,不適合依賴其他Bean的復(fù)雜場(chǎng)景。
2.使用 CommandLineRunner 接口
CommandLineRunner 接口提供了一個(gè) run 方法,該方法會(huì)在Spring Boot應(yīng)用啟動(dòng)后執(zhí)行。
@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("MyStartupRunner CommandLineRunner initialized~~~");
}
}
優(yōu)點(diǎn):
- 簡(jiǎn)單易用:實(shí)現(xiàn) CommandLineRunner 接口并重寫 run 方法即可。
- 適合處理命令行參數(shù):可以直接訪問命令行參數(shù)(String… args)。
- 順序可控:可以通過 @Order 注解或?qū)崿F(xiàn) Ordered 接口來控制多個(gè) CommandLineRunner 的執(zhí)行順序。
缺點(diǎn):
- 執(zhí)行時(shí)機(jī)較早:在Spring應(yīng)用上下文刷新完成后執(zhí)行,但此時(shí)可能某些Bean尚未完全初始化完成,不適合依賴某些復(fù)雜Bean的場(chǎng)景。
3.使用 ApplicationRunner 接口
ApplicationRunner 接口與 CommandLineRunner 類似,但它提供了更豐富的 ApplicationArguments 參數(shù)來處理命令行參數(shù)。
@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("MyApplicationRunner initialized~~~");
}
}
優(yōu)點(diǎn):
- 更強(qiáng)大的參數(shù)處理:提供了 ApplicationArguments 對(duì)象,可以更方便地解析命令行參數(shù)(如 --key=value 格式)。
- 順序可控:同樣可以通過 @Order 注解或?qū)崿F(xiàn) Ordered 接口來控制執(zhí)行順序。
缺點(diǎn):
- 執(zhí)行時(shí)機(jī)較早:與CommandLineRunner類似,可能某些Bean尚未完全初始化完成,不適合依賴某些復(fù)雜Bean的場(chǎng)景。
4.使用 ApplicationListener 監(jiān)聽 ApplicationReadyEvent
@Component
@Slf4j
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("MyApplicationListener initialized~~~");
}
}
優(yōu)點(diǎn):
- 確保應(yīng)用完全啟動(dòng):
ApplicationReadyEvent是在應(yīng)用完全啟動(dòng)后觸發(fā)的,適合執(zhí)行依賴其他Bean或外部資源的邏輯。 - 靈活性強(qiáng):可以監(jiān)聽其他Spring事件(如
ContextRefreshedEvent),適合更復(fù)雜的場(chǎng)景。
缺點(diǎn):
- 無法直接訪問命令行參數(shù):如果需要處理命令行參數(shù),需要額外處理。
5.@EventListener監(jiān)聽ApplicationReadyEvent
可以使用 @EventListener 注解來監(jiān)聽 ApplicationReadyEvent 事件
@Component
@Slf4j
public class MyEventListener {
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
log.info("MyEventListener initialized~~~");
}
}
優(yōu)點(diǎn):
- 簡(jiǎn)潔:使用注解方式監(jiān)聽事件,代碼更簡(jiǎn)潔。
- 確保應(yīng)用完全啟動(dòng):與 ApplicationListener 相同,適合在應(yīng)用完全啟動(dòng)后執(zhí)行邏輯。
- 靈活性強(qiáng):可以監(jiān)聽多個(gè)事件。
缺點(diǎn):
- 無法直接訪問命令行參數(shù):同樣需要額外處理命令行參數(shù)。
- 執(zhí)行順序不可控:多個(gè)監(jiān)聽器的執(zhí)行順序不可控。
以上幾種方式執(zhí)行后日志打印如下:
[main] com.example.springdemo.init.MyInit : PostConstruct initialized~~~ [main] c.e.springdemo.SpringDemoApplication : Started SpringDemoApplication in 0.358 seconds (JVM running for 0.809) [main] c.e.springdemo.init.MyApplicationRunner : MyApplicationRunner initialized~~~ [main] c.e.springdemo.init.MyCommandLineRunner : CommandLineRunner initialized~~~ [main] c.e.s.init.MyApplicationListener : MyApplicationListener initialized~~~ [main] c.e.springdemo.init.MyEventListener : MyEventListener initialized~~~
總結(jié)
- 如果需要處理命令行參數(shù),優(yōu)先選擇
CommandLineRunner或ApplicationRunner。 - 如果只是簡(jiǎn)單的Bean初始化邏輯,使用
@PostConstruct。 - 如果需要在應(yīng)用完全啟動(dòng)后執(zhí)行邏輯,選擇
ApplicationListener或@EventListener。 - 如果需要更靈活的事件監(jiān)聽機(jī)制,選擇
ApplicationListener或@EventListener。
以上就是在SpringBoot啟動(dòng)時(shí)執(zhí)行特定代碼的常見方法小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟動(dòng)執(zhí)行特定代碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot自帶定時(shí)任務(wù)實(shí)現(xiàn)動(dòng)態(tài)配置Cron參數(shù)方式
這篇文章主要介紹了Springboot自帶定時(shí)任務(wù)實(shí)現(xiàn)動(dòng)態(tài)配置Cron參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼
這篇文章主要介紹了java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
RxJava2 Scheduler使用實(shí)例深入解析
這篇文章主要為大家介紹了RxJava2 Scheduler使用實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解,如果想在應(yīng)用中使用Quartz任務(wù)調(diào)度功能,可以通過Spring Boot實(shí)現(xiàn)Quartz的自動(dòng)配置,以下介紹如何開啟Quartz自動(dòng)配置,以及Quartz自動(dòng)配置的實(shí)現(xiàn)過程,需要的朋友可以參考下2023-11-11
Idea如何導(dǎo)入java mysql驅(qū)動(dòng)包
本文介紹了如何在IntelliJ IDEA中配置MySQL數(shù)據(jù)庫(kù)連接,首先下載MySQL Connector/J驅(qū)動(dòng)并解壓,然后在Idea項(xiàng)目中創(chuàng)建lib文件夾并將.jar文件復(fù)制到該文件夾,接著,將.jar文件添加為項(xiàng)目庫(kù),通過這些步驟,可以成功配置MySQL數(shù)據(jù)庫(kù)連接2024-12-12
Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
將Java應(yīng)用做成exe可執(zhí)行軟件的流程步驟
這篇文章主要介紹了如何將Java程序打包成可執(zhí)行的exe文件,通過配置Maven、編寫批處理文件、使用Bat_To_Exe_Converter工具將批處理文件轉(zhuǎn)換為exe文件,實(shí)現(xiàn)雙擊啟動(dòng)的效果,感興趣的小伙伴跟著小編一起來看看吧2025-04-04

