SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式
在 Spring Boot 項目中,可以通過以下幾種方式實現(xiàn) 在項目啟動完成后自動加載系統(tǒng)配置緩存操作 的需求:
1. 使用 CommandLineRunner
CommandLineRunner 是一個接口,可以用來在 Spring Boot 應用啟動后立即執(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í)行時機稍早于項目完全啟動完成,因此需要配合延時操作來確保項目完全啟動后再執(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 提供了更靈活的控制,可以控制代碼的啟動和停止時機。
實現(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 初始化完成。
- 推薦使用
需要更細粒度的控制:
- 使用
SmartLifecycle提供更靈活的控制。
- 使用
以上就是SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式的詳細內(nèi)容,更多關(guān)于SpringBoot自動加載系統(tǒng)配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring cloud gateway 限流的實現(xiàn)與原理
這篇文章主要介紹了spring cloud gateway 限流的實現(xiàn)與原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
SpringBoot選擇自有bean優(yōu)先加載實現(xiàn)方法
在一些需求中,可能存在某些場景,比如先加載自己的bean,然后自己的bean做一些DB操作,初始化配置問題,然后后面的bean基于這個配置文件,繼續(xù)做其他的業(yè)務邏輯。因此有了本文的這個題目2023-03-03
使用Spring?Boot快速構(gòu)建一個簡單的文件處理工具
在現(xiàn)代Web應用中,文件上傳與處理是常見的需求,本文將通過一個實際案例,詳細介紹如何使用Spring?Boot構(gòu)建一個文件處理工具,感興趣的小伙伴可以參考一下2025-06-06
mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果的實例代碼
這篇文章主要介紹了mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01
springboot?vue項目管理后端實現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項目管理后端實現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

