/** * 实际运行执行顺序总结: * 1. 序1: @PostConstruct 、 序2:InitializingBean.afterPropertiesSet()、序3:@Bean(initMethod = "init"):在 Bean 初始化完成后执行,优先级最高。 * * 2.序4:SmartInitializingSingleton:在所有单例 Bean 初始化完成后执行。 * * 3.序5:@EventListener:根据监听的事件类型决定执行时机,例如 ContextRefreshedEvent 通常在应用启动完成后触发。 * * 4.序6:ApplicationRunner 和 序7:CommandLineRunner:在 Spring Boot 应用启动完成后执行,@Order 决定它们的执行顺序。 * * * 选择建议 * 1.如果是单个 Bean 的初始化,优先使用 @PostConstruct 或 InitializingBean。 * * 2.如果是全局初始化逻辑,优先使用 CommandLineRunner 或 ApplicationRunner。 * * 3.如果需要监听特定事件,使用 @EventListener。 * * 4.如果需要在所有单例 Bean 初始化完成后执行,使用 SmartInitializingSingleton。 */
上代码:
/** 一 * 在 Spring Boot 应用中,CommandLineRunner、ApplicationRunner 和 @PostConstruct 都可以用于在应用启动时执行一些初始化逻辑。 * 它们的执行顺序如下: * * 执行顺序 * 1.@PostConstruct * @PostConstruct 是 Java 提供的注解,用于在 Bean 初始化完成后立即执行。 * 它的执行时机是在 Spring 容器完成依赖注入(即所有 @Autowired 或构造函数注入完成)之后,但在 CommandLineRunner 和 ApplicationRunner 之前。 * 优先级最高。 * * 2.CommandLineRunner 和 ApplicationRunner * 这两个接口的实现类会在 Spring Boot 应用完全启动后执行。 * 它们的执行顺序取决于 @Order 注解或实现 Ordered 接口的顺序。 * 如果没有指定顺序,默认情况下,CommandLineRunner 和 ApplicationRunner 的执行顺序是不确定的。 * * * @Author:hw0089751 * @Date:2025/12/10 */ @Component public class CaffeineManualExamplePostConstruct { /** * 1. @PostConstruct * 执行时机:Bean 初始化完成后,Spring 容器启动过程中。 * 适用场景:用于初始化与当前 Bean 相关的资源或逻辑。 * * @throws Exception */ @PostConstruct public void init() throws Exception { System.out.println("初始化============执行 @PostConstruct 的 init 方法"); } } // 二 @Component public class MyInitBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("初始化============执行 InitializingBean 的 afterPropertiesSet 方法"); } } // 三 @Configuration public class AppConfig { @Bean(initMethod = "init") public MyBean myBean() { return new MyBean(); } } public class MyBean { public void init() { System.out.println("初始化============执行 @Bean 的 initMethod 方法"); } } // 四 @Component public class MySmartInitializingSingleton implements SmartInitializingSingleton { @Override public void afterSingletonsInstantiated() { System.out.println("初始化============执行 SmartInitializingSingleton 的 afterSingletonsInstantiated 方法"); } } // 五 @Component public class MyEventListener { @EventListener public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("初始化============执行 @EventListener 的 onApplicationEvent 方法"); } } // 六 /** * 3. ApplicationRunner * 执行时机:与 CommandLineRunner 相同,Spring Boot 应用启动完成后执行。 * 适用场景:与 CommandLineRunner 类似,但 ApplicationRunner 提供了更强大的参数解析功能。 * * @Author:hw0089751 * @Date:2025/12/10 */ @Component public class CaffeineManualExampleApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("初始化============执行 ApplicationRunner 的 run 方法"); } } package com.example.demo.runner; import com.example.demo.caffe3.CaffeineService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; // 七 /** * Caffeine 本地缓存 * * Caffeine 是一个高性能的 Java 缓存库,主要用于实现本地缓存。它提供了灵活的配置选项和强大的功能, * 适合各种场景的缓存需求。 * * 核心功能: * 1基于大小的缓存驱逐:可以设置缓存的最大容量,超出容量时会自动驱逐旧数据。 * 2基于时间的缓存驱逐:支持设置数据的过期时间,例如固定时间后自动移除。 * 3基于权重的缓存驱逐:可以根据权重来决定缓存的驱逐策略。 * 4异步加载:支持异步加载数据,提升性能。 * 5统计功能:可以统计缓存的命中率、加载时间等指标。 * * 使用场景: * 1本地缓存:适合存储频繁访问的热点数据,减少数据库或远程服务的访问压力。 * 2短期数据存储:存储临时数据,例如会话信息、配置数据等。 * 3性能优化:通过缓存减少计算或查询的开销。 * */ @Component public class CaffeineManualExampleCommandLineRunner implements CommandLineRunner { /** * 2. CommandLineRunner * 执行时机:Spring Boot 应用启动完成后,run 方法会被调用。 * 适用场景:用于在应用启动后执行一些全局的初始化逻辑。 */ @Override public void run(String... args) throws Exception { System.out.println("初始化============执行 CommandLineRunner 的 run 方法"); } }