springboot启动时多次监听到ApplicationReadyEvent事件

现象

版本说明:
springboot:2.2.2.RELEASE
springcloud:2.2.2.RELEASE
测试代码:

@Slf4j
public class ReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
    static final AtomicInteger count = new AtomicInteger();

    static {
        log.info("static test");
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("监听到ready事件,第{}次", count.incrementAndGet());
    }
}
//在META-INFO文件下,spring.factories文件中加入
org.springframework.context.ApplicationListener=com.demo.springbootdemo.event.ReadyEventListener

测试结果:
共监听到三次ready事件。
共监听到三次ready事件

解决方案

以下每一项修改都可以解决:

  1. 将ReadyEventListener使用@Component注入,而非通过spring.factories注入。
  2. 移除springcloud依赖
  3. 升级springboot/springcloud版本(springboot 2.4及以上)

出现原因

旧版springcloud(2.x.x)带来的问题:

旧版springcloud(2.x.x)运行时会优先于springboot进行一次content初始化,并发布一次ready事件。加上后续的springboot初始化content,会发布第二次ready事件。

  1. 初始化content前发布start event
    在这里插入图片描述
    在这里插入图片描述
  2. prerare environment时发布prepare事件
    在这里插入图片描述
  3. springcloud 监听prepare事件,调用自己的content生成方法
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    此时content的beanfactory中仅有少量的与springcloud相关的bean被初始化:
    在这里插入图片描述
    随后便第一次发布了ready 事件
    在这里插入图片描述
    在这里插入图片描述

此后回到Springboot初始化content动作(从null开始):第二次发布ready事件。(补充:springcloud 初始化后的content会被封装到Initializer中,在后续被重新释放出来。)
在这里插入图片描述

spring.factories带来的问题:

使用component注册ReadyEventListener并不会出现问题:该bean在spring boot容器初始化后才处理,此时只会监听到一次ready事件。

spring.factories注册优先级极高,会导致其指定的bean优先初始化(优先于content初始化),从而监听到两次ready事件。
在这里插入图片描述
对于ApplicationContextInitializer.class与ApplicationListener.class在应用run之前便已初始化

新版Springboot解决方案:

直接为springcloud定制化开发初始化语句,后续通过listener导入到content中。
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中,我们可以通过实现监听器接口来监听启动事件SpringBoot提供了两个监听器接口:CommandLineRunner和ApplicationRunner。这两个接口都需要标注@Component注解,并由IOC容器进行管理。在SpringBoot启动成功后,这两个接口的实现类中的回调方法会被执行。 具体来说,CommandLineRunner接口中有一个run方法,它会在所有的Bean都初始化之后、SpringApplication.run方法执行完成之前被调用。而ApplicationRunner接口中也有一个run方法,它会在CommandLineRunner接口的run方法执行完之后被调用。这意味着ApplicationRunner接口的实现类会在SpringBoot启动之后的最后阶段执行。 通过实现这两个接口,我们可以在SpringBoot启动成功后执行一些回调方法,用于资源的初始化或其他服务的启动。监听器的执行顺序可以根据实现类的顺序来确定,实现类的顺序会影响回调方法的执行顺序。因此,我们可以根据业务需求,在不同的节点插入想要实现的代码,例如在服务器启动后加载缓存或动态修改环境变量中的参数等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot的启动监听](https://blog.csdn.net/jishanwang/article/details/88100499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot各个监听器启动执行顺序](https://blog.csdn.net/zhaojiyuan1024/article/details/124246752)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值