Spring事件机制源码分析

5次阅读

共计 2004 个字符,预计需要花费 6 分钟才能阅读完成。

前言
由于之前使用 Spring 事件机制出现了问题,所以特意去了解这块的源码。Spring 事件机制其实就是事件发布 / 订阅(注意在 Spring 中订阅指的是监听)。
PS:Spring 版本为 5.1.5.RELEASE
源码分析
初始化
初始化这块关键是核心组件的注册

ApplicationEventPublisher 的初始化与注册,关键方法为 AbstractApplicationContext 的方法 prepareBeanFactory()

ApplicationEventMulticaster 的初始化与注册,关键方法为 AbstractApplicationContext 的 initApplicationEventMulticaster() 方法

ApplicationListener 的初始化与注册,关键方法为 AbstractApplicationContext 的 registerListeners() 方法

这块不细说,感兴趣的可以自行跟踪关键方法
事件发布 / 订阅
事件发布 / 订阅的关键方法为 AbstractApplicationContext 的 publishEvent,源码如下:
protected void publishEvent(Object event, ResolvableType eventType) {
// 避免空指针
Assert.notNull(event, “Event must not be null”);
if (logger.isTraceEnabled()) {
logger.trace(“Publishing event in ” + getDisplayName() + “: ” + event);
}

// 处理 event 对象,将其转换为 ApplicationEvent
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<Object>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
}
}

// 是否延迟多播,即将事件发布到所有监听器中
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
// 此处为事件监听处理器的调用关键
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}

// 是否将事件发布到父容器中
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}
通过代码跟踪,发现 Spring 中使用 ApplicationEventMulticaster 的默认实现 SimpleApplicationEventMulticaster 来触发事件的监听,关键方法为 multicastEvent() 方法,源码如下:
@Override
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
// 获取事件类型
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {// 依次遍历事件监听器
// 获取线程池
Executor executor = getTaskExecutor();
if (executor != null) {// 线程池不为 null,则异步调用监听器
executor.execute(new Runnable() {
@Override
public void run() {
invokeListener(listener, event);
}
});
}
else {// 同步调用监听器
invokeListener(listener, event);
}
}
}

正文完
 0