栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot应用事件监听示例详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot应用事件监听示例详解

前言

本文主要给大家介绍了关于Spring Boot应用事件监听的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

1. Spring Boot特有的应用事件

除了Spring框架的事件,Spring Boot的SpringApplication也发送了一些自己的事件:

  • ApplicationStartingEvent:在任何处理(除了注册listener和initializer)开始之前发送。
  • ApplicationEnvironmentPreparedEvent: 在context创建之前,而用到context中的Environment已经被识别时发送。
  • ApplicationContextInitializedEvent: SpringApplication正在启动,ApplicationContext已准备好且ApplicationContextInitializer已被调用但是bean的定义还没有被加载时发送。
  • ApplicationPreparedEvent: 在context刷新之前,在bean的定义已经被加载之后调用。
  • ApplicationStartedEvent: 在任何应用和command-line runner调用之前,而context已经被刷新时发送。
  • ApplicationReadyEvent: 在任何应用和command-line runner被调用的时候发送,它意味着应用可以接受请求了。
  • ApplicationFailedEvent: 在启动时有异常的时候发送。

有些事件是在ApplicationContext创建之前触发的,所以我们不能用常规的注册成bean的事件监听方式:

  • 注解了@EventListener注解分方法的类注册的bean;
  • 实现了ApplicationListener接口的类注册的bean。

像ApplicationStartedEvent和ApplicationReadyEvent是ApplicationContext创建之后触发的,可以用上述两种方式来监听事件。

2. 如何监听这些事件

我们可以通过下面的方式注册监听:

2.1. SpringApplication.addListeners(...)

SpringApplication application = new SpringApplication(StartEventsApplication.class);
application.addListeners(
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
  (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName())
);
application.run(args);

2.2. SpringApplicationBuilder.listeners(...)

new SpringApplicationBuilder()
   .sources(StartEventsApplication.class)
   .listeners(
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName())
     )
   .run(args);

2.3. meta-INF/spring.factories

src/main/resources/meta-INF/spring.factories:

org.springframework.context.ApplicationListener=top.wisely.startevents.listeners.ApplicationContextInitializedEventListener, 
     top.wisely.startevents.listeners.ApplicationEnvironmentPreparedEventListener, 
     top.wisely.startevents.listeners.ApplicationPreparedEventListener, 
     top.wisely.startevents.listeners.ApplicationReadyEventListener, 
     top.wisely.startevents.listeners.ApplicationStartedEventListener, 
     top.wisely.startevents.listeners.ApplicationStartingEventListener

监听器只需实现ApplicationListener<要监听的接口类型>接口,无需手动注册为bean:

public class ApplicationStartedEventListener implements ApplicationListener {
 @Override
 public void onApplicationEvent(ApplicationStartedEvent event) {
  log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName());
 }
}

3. 源码地址

https://github.com/wiselyman/spring-boot-application-events.git (本地下载)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号