煙臺優(yōu)化網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷好不好
Spring Boot 項(xiàng)目啟動后自動加載系統(tǒng)配置的多種實(shí)現(xiàn)方式
在 Spring Boot 項(xiàng)目中,可以通過以下幾種方式實(shí)現(xiàn) 在項(xiàng)目啟動完成后自動加載系統(tǒng)配置緩存操作 的需求:
1. 使用 CommandLineRunner
CommandLineRunner
是一個(gè)接口,可以用來在 Spring Boot 應(yīng)用啟動后立即執(zhí)行一些邏輯代碼。
實(shí)現(xiàn)方式:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class SystemConfigLoader implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 在這里加載系統(tǒng)配置緩存System.out.println("項(xiàng)目啟動完成,開始加載系統(tǒng)配置...");// 模擬加載配置操作loadSystemConfig();}private void loadSystemConfig() {// 假設(shè)從數(shù)據(jù)庫中加載配置System.out.println("系統(tǒng)配置加載成功!");}
}
2. 使用 ApplicationRunner
ApplicationRunner
與 CommandLineRunner
類似,但支持接收一個(gè) ApplicationArguments
對象,用于更靈活地處理傳入?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 {@Overridepublic void run(ApplicationArguments args) throws Exception {// 在這里加載系統(tǒng)配置緩存System.out.println("項(xià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 完成所有啟動流程后執(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)目啟動完成后加載系統(tǒng)配置System.out.println("項(xiàng)目啟動完成,開始加載系統(tǒng)配置...");loadSystemConfig();}private void loadSystemConfig() {// 假設(shè)從數(shù)據(jù)庫中加載配置System.out.println("系統(tǒng)配置加載成功!");}
}
4. 使用 @PostConstruct
注解
@PostConstruct
注解會在 Bean 初始化后執(zhí)行,但其執(zhí)行時(shí)機(jī)稍早于項(xiàng)目完全啟動完成,因此需要配合延時(shí)操作來確保項(xiàng)目完全啟動后再執(zhí)行。
實(shí)現(xiàn)方式:
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Component;@Component
public class SystemConfigLoader {@PostConstructpublic void init() {// 延時(shí)加載以確保項(xiàng)目完全啟動new Thread(() -> {try {Thread.sleep(2000); // 模擬延時(shí)System.out.println("項(xià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
提供了更靈活的控制,可以控制代碼的啟動和停止時(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;@Overridepublic void start() {// 項(xiàng)目啟動完成后執(zhí)行邏輯System.out.println("項(xiàng)目啟動完成,開始加載系統(tǒng)配置...");loadSystemConfig();running = true;}@Overridepublic void stop() {// 停止邏輯(可選)System.out.println("項(xiàng)目停止時(shí)執(zhí)行清理工作...");}@Overridepublic boolean isRunning() {return running;}private void loadSystemConfig() {// 模擬加載配置操作System.out.println("系統(tǒng)配置加載成功!");}
}
對比與推薦
-
簡單場景:
- 推薦使用
CommandLineRunner
或ApplicationRunner
,實(shí)現(xiàn)簡單且清晰。
- 推薦使用
-
更靈活的監(jiān)聽啟動事件:
- 推薦使用
@EventListener
監(jiān)聽ApplicationReadyEvent
,可以確保所有 Bean 初始化完成。
- 推薦使用
-
需要更細(xì)粒度的控制:
- 使用
SmartLifecycle
提供更靈活的控制。
- 使用