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