在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):
- 簡單直接:只需在方法上添加 @PostConstruct 注解,方法會(huì)在Bean初始化后立即執(zhí)行。
- 靈活性高:可以在任何Bean中使用,適合對(duì)特定Bean進(jìn)行初始化邏輯。
缺點(diǎn):
- 無法控制執(zhí)行順序:多個(gè) @PostConstruct 方法的執(zhí)行順序不可控。
- 不適合復(fù)雜啟動(dòng)邏輯:在Bean初始化完成后執(zhí)行,但此時(shí)可能其他Bean尚未完全初始化完成,不適合依賴其他Bean的復(fù)雜場景。
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):
- 簡單易用:實(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的場景。
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的場景。
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ù)雜的場景。
缺點(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)聽事件,代碼更簡潔。
- 確保應(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。 - 如果只是簡單的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)文章
通過實(shí)例解析POJO和JavaBean的區(qū)別
這篇文章主要介紹了通過實(shí)例解析POJO和JavaBean的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Spring Boot Logging Level設(shè)置為off時(shí)的Bug
這篇文章主要介紹了Spring Boot Logging Level設(shè)置為off時(shí)的Bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
mybatis-plus使用generator實(shí)現(xiàn)逆向工程
mybatis-plus-generator在3.5.0以及以后的版本使用新的方式逆向生成代碼,本文主要介紹了mybatis-plus使用generator實(shí)現(xiàn)逆向工程,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
Springboot服務(wù)實(shí)現(xiàn)執(zhí)行SQL腳本文件
這篇文章主要介紹了Springboot服務(wù)實(shí)現(xiàn)執(zhí)行SQL腳本文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot集成Redis—使用RedisRepositories詳解
這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實(shí)現(xiàn)WGS84/百度/騰訊/高德等主流的地理坐標(biāo)轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)WGS84,百度,騰訊,高德等主流的地理坐標(biāo)轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-12-12

