SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式
在 Spring Boot 項目中,可以通過以下幾種方式實現(xiàn) 在項目啟動完成后自動加載系統(tǒng)配置緩存操作 的需求:
1. 使用 CommandLineRunner
CommandLineRunner 是一個接口,可以用來在 Spring Boot 應(yīng)用啟動后立即執(zhí)行一些邏輯代碼。
實現(xiàn)方式:
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class SystemConfigLoader implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 在這里加載系統(tǒng)配置緩存 System.out.println("項目啟動完成,開始加載系統(tǒng)配置..."); // 模擬加載配置操作 loadSystemConfig(); } private void loadSystemConfig() { // 假設(shè)從數(shù)據(jù)庫中加載配置 System.out.println("系統(tǒng)配置加載成功!"); } }
2. 使用 ApplicationRunner
ApplicationRunner
與 CommandLineRunner
類似,但支持接收一個 ApplicationArguments
對象,用于更靈活地處理傳入?yún)?shù)。
實現(xiàn)方式:
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class SystemConfigLoader implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 在這里加載系統(tǒng)配置緩存 System.out.println("項目啟動完成,開始加載系統(tǒng)配置..."); loadSystemConfig(); } private void loadSystemConfig() { // 假設(shè)從數(shù)據(jù)庫中加載配置 System.out.println("系統(tǒng)配置加載成功!"); } }
3. 使用 @EventListener 監(jiān)聽 ApplicationReadyEvent
通過監(jiān)聽 ApplicationReadyEvent
,可以在 Spring Boot 完成所有啟動流程后執(zhí)行邏輯。
實現(xiàn)方式:
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class SystemConfigLoader { @EventListener(ApplicationReadyEvent.class) public void onApplicationReady() { // 在項目啟動完成后加載系統(tǒng)配置 System.out.println("項目啟動完成,開始加載系統(tǒng)配置..."); loadSystemConfig(); } private void loadSystemConfig() { // 假設(shè)從數(shù)據(jù)庫中加載配置 System.out.println("系統(tǒng)配置加載成功!"); } }
4. 使用 @PostConstruct 注解
@PostConstruct
注解會在 Bean 初始化后執(zhí)行,但其執(zhí)行時機(jī)稍早于項目完全啟動完成,因此需要配合延時操作來確保項目完全啟動后再執(zhí)行。
實現(xiàn)方式:
import jakarta.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class SystemConfigLoader { @PostConstruct public void init() { // 延時加載以確保項目完全啟動 new Thread(() -> { try { Thread.sleep(2000); // 模擬延時 System.out.println("項目啟動完成,開始加載系統(tǒng)配置..."); loadSystemConfig(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }).start(); } private void loadSystemConfig() { // 假設(shè)從數(shù)據(jù)庫中加載配置 System.out.println("系統(tǒng)配置加載成功!"); } }
5. 使用 SmartLifecycle 接口
SmartLifecycle
提供了更靈活的控制,可以控制代碼的啟動和停止時機(jī)。
實現(xiàn)方式:
import org.springframework.context.SmartLifecycle; import org.springframework.stereotype.Component; @Component public class SystemConfigLoader implements SmartLifecycle { private boolean running = false; @Override public void start() { // 項目啟動完成后執(zhí)行邏輯 System.out.println("項目啟動完成,開始加載系統(tǒng)配置..."); loadSystemConfig(); running = true; } @Override public void stop() { // 停止邏輯(可選) System.out.println("項目停止時執(zhí)行清理工作..."); } @Override public boolean isRunning() { return running; } private void loadSystemConfig() { // 模擬加載配置操作 System.out.println("系統(tǒng)配置加載成功!"); } }
對比與推薦
簡單場景:
- 推薦使用
CommandLineRunner
或ApplicationRunner
,實現(xiàn)簡單且清晰。
- 推薦使用
更靈活的監(jiān)聽啟動事件:
- 推薦使用
@EventListener
監(jiān)聽ApplicationReadyEvent
,可以確保所有 Bean 初始化完成。
- 推薦使用
需要更細(xì)粒度的控制:
- 使用
SmartLifecycle
提供更靈活的控制。
- 使用
以上就是SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot自動加載系統(tǒng)配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot整合Springsecurity實現(xiàn)數(shù)據(jù)庫登錄及權(quán)限控制功能
本教程詳細(xì)介紹了如何使用SpringBoot整合SpringSecurity實現(xiàn)數(shù)據(jù)庫登錄和權(quán)限控制,本文分步驟結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-10-10通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis)
這篇文章主要介紹了通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法
這篇文章主要介紹了構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05spring的事務(wù)傳播屬性REQUIRED_NESTED原理
這篇文章主要介紹了spring的事務(wù)傳播屬性REQUIRED_NESTED原理,在spring中,要想使用事務(wù)中的回滾點,可以使用傳播屬性NESTED,需要的朋友可以參考下2023-05-05Springboot+MybatisPlus實現(xiàn)帶驗證碼的登錄
本文主要介紹了Springboot+MybatisPlus實現(xiàn)帶驗證碼的登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05springboot2中session超時,退到登錄頁面方式
這篇文章主要介紹了springboot2中session超時,退到登錄頁面方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01