SpringBoot 项目启动后监听器执行操作:ContextRefreshEvent
一 Spring boot运行时,会依次发送以下事件
1. ApplicationStartingEvent
2. ApplicationEnvironmentPreparedEvent:当Environment已经准备好,在context 创建前
3. ApplicationContextInitializedEvent:在ApplicationContext 创建和ApplicationContextInitializer都被调用后,但是bean definition没有被加载前
4. ApplicationPreparedEvent:bean definition已经加载,但是没有refresh
5. ApplicationStartedEvent: context 已经被refresh, 但是application 和command-line 的runner都没有被调用
6. AvailabilityChangeEvent
7. ApplicationReadyEvent: application 和command-line 的runner都被调用后触发
8. AvailabilityChangeEvent
9. ApplicationFailedEvent: 启动失败触发
另外,会在 ApplicationPreparedEvent 之后和 ApplicationStartedEvent 之前发送 ContextRefreshedEvent 事件
二 项目启动后需要执行某个操作
1. 实现 ApplicationListener<E extends ApplicationEvent>接口
2. ApplicationEvent 的子类可以是 ApplicationReadyEvent 或者 ContextRefreshedEvent
3. ApplicationReadyEvent 的示例
@Component @Slf4j public class ApplicationInit implements ApplicationListener<ApplicationReadyEvent> { // 项目启动后预热JSONObject @Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { UserInfo userInfo = new UserInfo(); userInfo.setId(123L); userInfo.setChannel("hello"); String userJson = JSON.toJSONString(userInfo); JSONObject.parseObject(userJson, UserInfo.class); } }
三 ContextRefreshedEvent 多次执行的问题
1. web应用会出现父子容器,这样就会触发两次
2. 解决方法:ApplicationListener<ContextRefreshedEvent> 应该和 ApplicationContext 一对一
@Order @Component @Slf4j public class ApplicationInit implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // SpringBoot 项目区启动后,监听器执行对应事件的逻辑 UserInfo userInfo = new UserInfo(); userInfo.setId(123L); userInfo.setChannel("hello"); String userJson = JSON.toJSONString(userInfo); JSONObject.parseObject(userJson, UserInfo.class); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了