前言
先看下Spring官方文档对于事件以及监听器的解释与说明。
监听器官方说明

总结起来就是几点:
- 除了通常的Spring框架自带的事件例如:ContextRefreshedEvent,SpringApplication还会发送一些额外的事件。
- 对于事件的监听,需要通过监听器来实现。在SpringBoot中,监听器可以通过三种方式来注册,
① 通过SpringApplication.addListeners(…)
② 通过SpringApplicationBuilder.listeners(…)
③ 通过注册在META-INF/spring.factories中,示例为org.springframework.context.ApplicationListener=com.example.project.MyListener - 监听器不能以Bean的形式注册进SpringIOC容器中,因为监听器是在ApplicationContext上下文创建成功之前调用的。
- Spring应用程序的内置事件会以以下顺序发送:
① ApplicationStartingEvent
② ApplicationEnvironmentPreparedEvent
③ ApplicationContextInitializedEvent
④ ApplicationPreparedEvent
⑤ ApplicationStartedEvent
⑥ AvailabilityChangeEvent
⑦ ApplicationReadyEvent
⑧ AvailabilityChangeEvent
⑨ ApplicationFailedEvent
除了上面这些绑定在SpringApplication上的事件外,还有ContextRefreshedEvent和WebServerInitializedEvent、ServletWebServerInitializedEvent、ReactiveWebServerInitializedEvent等事件在ApplicationPreparedEvent和ApplicationStartedEvent事件之间发布。
在介绍完SpringBoot的官方文档后,下面看看SpringBoot源码中监听器是如何实现的。
正文
1. SpringBoot通过factories注册的监听器
在前面已经讲解过了SpringFactoriesLoader加载spring.factories的原理机制,大家都清楚了SpringFactoriesLoader会加载spring.factories中注册的初始化器、监听器、后置处理器等组件,在SpringApplication的构造方法中会通过JDK的反射工具实例化这些组件。

从源码中可以看到,SpringBoot在SpringApplication的构造方法中,会先获取ApplicationListener类型的监听器,并存进SpringApplication对象中,用于后续操作的调用。
下面看看ApplicationListener接口定义信息
/**
* Interface to be implemented by application event listeners.
*
* <p>Based on the standard {@code java.util.EventListener} interface
* for the Observer design pattern.
*
* <p>As of Spring 3.0, an {@code ApplicationListener} can generically declare
* the event type that it is interested in. When registered with a Spring
* {@code ApplicationContext}, events will be filtered accordingly, with the
* listener getting invoked for matching event objects only.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @param <E> the specific {@code ApplicationEvent} subclass to listen to
* @see org.springframework.context.ApplicationEvent
* @see org.springframework.context.event.ApplicationEventMulticaster
* @see org.springframework.context.event.EventListener
*/
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
注释大致意思为:
ApplicationListener接口是用于定义Spring应用程序监听器的接口,是基于Observer(观察者)设计模式的标准接口。从Spring3.0开始,ApplicationListener可以声明感兴趣的事件类型。
注册完spring.factories配置文件中的监听器后,SpringBoot框架是什么时候开始获取监听器然后调用监听器的监听的事件呢?下面来看SpringApplication#run方法。
重点需要关注下ApplicationListener#onApplicationEvent方法,这个方法是广播器播放监听器时调用的方法,详情会在下文中讲解。
SpringApplication#run

SpringApplication#getRunListeners

这个方法就是通过SpringFactoriesLoader去加载spring.factories配置文件中的文件指定的SpringApplicationRunListener类类型,这里获取的就是EventPublishingRunListener:
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

EventPublishingRunListener有什么作用?
1.1 EventPublishingRunListener
下面先看下EventPublishingRunListener源码
/**
* SpringApplicationRunListener 是用于发布 SpringApplicationEvent的。
* SpringApplicationRunListener通过内部的ApplicationEventMulticast

本文深入剖析SpringBoot事件监听机制,解析事件、监听器、广播器间的交互,演示自定义事件与监听器实现。
最低0.47元/天 解锁文章
4750





