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

所属分类: 软件编程 / java 阅读数: 20
收藏 0 赞 0 分享

前言

本文主要给大家介绍了关于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<Event>接口的类注册的bean。

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

2. 如何监听这些事件

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

2.1. SpringApplication.addListeners(...)

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

2.2. SpringApplicationBuilder.listeners(...)

new SpringApplicationBuilder()
   .sources(StartEventsApplication.class)
   .listeners(
     (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener<ApplicationReadyEvent>) 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<ApplicationStartedEvent> {
 @Override
 public void onApplicationEvent(ApplicationStartedEvent event) {
  log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName());
 }
}

3. 源码地址

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

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

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多