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

时间:2021-07-11 14:42:59

前言

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

?
1
2
3
4
5
6
7
8
9
10
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(...)

?
1
2
3
4
5
6
7
8
9
10
11
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:

?
1
2
3
4
5
6
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:

?
1
2
3
4
5
6
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/