深入浅出SpringBoot源码——监听器与事件机制

本文深入剖析SpringBoot事件监听机制,解析事件、监听器、广播器间的交互,演示自定义事件与监听器实现。

前言

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

在这里插入图片描述
总结起来就是几点:

  1. 除了通常的Spring框架自带的事件例如:ContextRefreshedEvent,SpringApplication还会发送一些额外的事件。
  2. 对于事件的监听,需要通过监听器来实现。在SpringBoot中,监听器可以通过三种方式来注册,
    ① 通过SpringApplication.addListeners(…)
    ② 通过SpringApplicationBuilder.listeners(…)
    ③ 通过注册在META-INF/spring.factories中,示例为org.springframework.context.ApplicationListener=com.example.project.MyListener
  3. 监听器不能以Bean的形式注册进SpringIOC容器中,因为监听器是在ApplicationContext上下文创建成功之前调用的。
  4. 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
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值