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

作者:wiselyman 时间:2022-06-12 22:39:02 

前言

本文主要给大家介绍了关于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 (本地下载)

来源:http://www.wisely.top/2018/12/04/spring-boot-application-events/

标签:springboot,事件,监听
0
投稿

猜你喜欢

  • Spring Boot中的Properties的使用详解

    2021-07-02 07:25:22
  • Java面试题-实现复杂链表的复制代码分享

    2023-11-23 20:05:39
  • C# winform程序读取文本中的值实例讲解

    2023-03-17 21:36:51
  • Java通过Fork/Join优化并行计算

    2023-01-27 21:28:36
  • JAVA 字符串加密、密码加密实现方法

    2023-11-28 04:08:09
  • 轻松学习C#的结构和类

    2023-12-10 13:46:19
  • 浅谈C# winForm 窗体闪烁的问题

    2023-06-10 09:46:07
  • 基于C#实现的木马程序实例详解

    2023-02-25 20:05:03
  • Android自动播放Banner图片轮播效果

    2022-09-06 05:55:42
  • java实现五子棋小游戏

    2021-12-25 06:58:56
  • Java实现List集合转树形结构的示例详解

    2021-11-11 10:48:33
  • Springboot Vue实现单点登陆功能示例详解

    2023-11-05 00:29:11
  • Android开发改变字体颜色方法

    2022-11-10 05:51:01
  • c#调用c++的DLL的实现方法

    2023-10-27 05:27:40
  • c# winform取消右上角关闭按钮的实现方法

    2023-05-05 18:56:28
  • Android自定义textview实现竖直滚动跑马灯效果

    2023-09-30 07:42:05
  • android 判断横竖屏问题的详解

    2022-07-28 08:37:13
  • 浅入浅出的讲解Spring循环依赖问题

    2023-11-03 07:16:11
  • C++指向类成员函数的指针详细解析

    2022-03-05 04:50:28
  • Eclipse配置Tomcat和JDK步骤图解

    2022-11-23 11:46:54
  • asp之家 软件编程 m.aspxhome.com