Spring Boot 项目启动后自动加载系统配置的多种实现方式
在 Spring Boot 项目中,可以通过以下几种方式实现 在项目启动完成后自动加载系统配置缓存操作 的需求:
1. 使用 CommandLineRunner
 
CommandLineRunner 是一个接口,可以用来在 Spring Boot 应用启动后立即执行一些逻辑代码。
实现方式:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class SystemConfigLoader implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 在这里加载系统配置缓存System.out.println("项目启动完成,开始加载系统配置...");// 模拟加载配置操作loadSystemConfig();}private void loadSystemConfig() {// 假设从数据库中加载配置System.out.println("系统配置加载成功!");}
}
2. 使用 ApplicationRunner
 
ApplicationRunner 与 CommandLineRunner 类似,但支持接收一个 ApplicationArguments 对象,用于更灵活地处理传入参数。
实现方式:
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 {// 在这里加载系统配置缓存System.out.println("项目启动完成,开始加载系统配置...");loadSystemConfig();}private void loadSystemConfig() {// 假设从数据库中加载配置System.out.println("系统配置加载成功!");}
}
3. 使用 @EventListener 监听 ApplicationReadyEvent
 
通过监听 ApplicationReadyEvent,可以在 Spring Boot 完成所有启动流程后执行逻辑。
实现方式:
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() {// 在项目启动完成后加载系统配置System.out.println("项目启动完成,开始加载系统配置...");loadSystemConfig();}private void loadSystemConfig() {// 假设从数据库中加载配置System.out.println("系统配置加载成功!");}
}
4. 使用 @PostConstruct 注解
 
@PostConstruct 注解会在 Bean 初始化后执行,但其执行时机稍早于项目完全启动完成,因此需要配合延时操作来确保项目完全启动后再执行。
实现方式:
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Component;@Component
public class SystemConfigLoader {@PostConstructpublic void init() {// 延时加载以确保项目完全启动new Thread(() -> {try {Thread.sleep(2000); // 模拟延时System.out.println("项目启动完成,开始加载系统配置...");loadSystemConfig();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}).start();}private void loadSystemConfig() {// 假设从数据库中加载配置System.out.println("系统配置加载成功!");}
}
5. 使用 SmartLifecycle 接口
 
SmartLifecycle 提供了更灵活的控制,可以控制代码的启动和停止时机。
实现方式:
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;@Component
public class SystemConfigLoader implements SmartLifecycle {private boolean running = false;@Overridepublic void start() {// 项目启动完成后执行逻辑System.out.println("项目启动完成,开始加载系统配置...");loadSystemConfig();running = true;}@Overridepublic void stop() {// 停止逻辑(可选)System.out.println("项目停止时执行清理工作...");}@Overridepublic boolean isRunning() {return running;}private void loadSystemConfig() {// 模拟加载配置操作System.out.println("系统配置加载成功!");}
}
对比与推荐
-  简单场景: - 推荐使用 CommandLineRunner或ApplicationRunner,实现简单且清晰。
 
- 推荐使用 
-  更灵活的监听启动事件: - 推荐使用 @EventListener监听ApplicationReadyEvent,可以确保所有 Bean 初始化完成。
 
- 推荐使用 
-  需要更细粒度的控制: - 使用 SmartLifecycle提供更灵活的控制。
 
- 使用