Spring 事件原理

一、事件

image.png
自定义事件继承 ApplicationEvent 即可

二、事件监听者

image.png
SmartApplicationListener

三、事件发布者

image.png

1. ApplicationEventPublisher

  • publishEvent(ApplicationEvent)
  • publishEvent(Object)

1.1. AbstractApplicationContext

调用了 ApplicationEventMulticaster 中的 multicastEvent 方法来具体实现发布事件给监听者的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
if (this.logger.isTraceEnabled()) {
this.logger.trace("Publishing event in " + this.getDisplayName() + ": " + event);
}

Object applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent)event;
} else {
applicationEvent = new PayloadApplicationEvent(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType();
}
}

// 调用了 ApplicationEventMulticaster 中的 multicastEvent 方法
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
} else {
this.getApplicationEventMulticaster().multicastEvent((ApplicationEvent)applicationEvent, eventType);
}

if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext)this.parent).publishEvent(event, eventType);
} else {
this.parent.publishEvent(event);
}
}

}

2. ApplicationEventMulticaster

定义了对 ApplicationListener 的维护操作(比如新增、 移除等)
以及将 ApplicationEvent 多播给可用 ApplicationListener 的操作

  • multicastEvent(ApplicationEvent)
  • multicastEvent(ApplicationEvent, ResolvableType)

2.1. SimpleApplicationEventMulticaster

步骤:

  1. 调用父类中的 getApplicationListeners (ApplicationEvent, ResolvableType) 获取相关监听器
  2. 遍历并执行各个监听器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @Override
    public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
    ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
    // 执行所有监听器
    for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
    Executor executor = getTaskExecutor();
    if (executor != null) {
    executor.execute(() -> invokeListener(listener, event));
    }
    else {
    invokeListener(listener, event);
    }
    }
    }

2.2. AbstractApplicationEventMulticaster

步骤:

  1. 从缓存中取监听器集合(包括预加载的监听器)
  2. 若无则检索监听器并加入缓存

思考:

  • 考虑并发(ConcurrentHashMap、synchronized)
  • 利用 Map 进行缓存
  • 对键和值分别进行包装(ListenerCacheKey、ListenerRetriever),可附加额外功能(可扩展性)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    // 使用 ConcurrentHashMap 对监听器进行缓存
    final Map<ListenerCacheKey, ListenerRetriever> retrieverCache = new ConcurrentHashMap<>(64);
    ...
    protected Collection<ApplicationListener<?>> getApplicationListeners(
    ApplicationEvent event, ResolvableType eventType) {

    Object source = event.getSource();
    Class<?> sourceType = (source != null ? source.getClass() : null);
    // 以事件类型和源类型作为缓存的键
    ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

    // Quick check for existing entry on ConcurrentHashMap...
    ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
    if (retriever != null) {
    return retriever.getApplicationListeners();
    }

    if (this.beanClassLoader == null ||
    (ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
    (sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
    // Fully synchronized building and caching of a ListenerRetriever
    synchronized (this.retrievalMutex) {
    retriever = this.retrieverCache.get(cacheKey);
    if (retriever != null) {
    return retriever.getApplicationListeners();
    }
    retriever = new ListenerRetriever(true);
    // 检索监听器
    Collection<ApplicationListener<?>> listeners =
    retrieveApplicationListeners(eventType, sourceType, retriever);
    this.retrieverCache.put(cacheKey, retriever);
    return listeners;
    }
    }
    else {
    // No ListenerRetriever caching -> no synchronization necessary
    return retrieveApplicationListeners(eventType, sourceType, null);
    }
    }

    /**
    * 根据给出的事件类型和源类型检索相关监听器
    */
    private Collection<ApplicationListener<?>> retrieveApplicationListeners(
    ResolvableType eventType, @Nullable Class<?> sourceType, @Nullable ListenerRetriever retriever) {

    LinkedList<ApplicationListener<?>> allListeners = new LinkedList<>();
    Set<ApplicationListener<?>> listeners;
    Set<String> listenerBeans;
    synchronized (this.retrievalMutex) {
    listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
    listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
    }
    for (ApplicationListener<?> listener : listeners) {
    if (supportsEvent(listener, eventType, sourceType)) {
    if (retriever != null) {
    retriever.applicationListeners.add(listener);
    }
    allListeners.add(listener);
    }
    }
    if (!listenerBeans.isEmpty()) {
    BeanFactory beanFactory = getBeanFactory();
    for (String listenerBeanName : listenerBeans) {
    try {
    Class<?> listenerType = beanFactory.getType(listenerBeanName);
    if (listenerType == null || supportsEvent(listenerType, eventType)) {
    ApplicationListener<?> listener =
    beanFactory.getBean(listenerBeanName, ApplicationListener.class);
    if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
    if (retriever != null) {
    retriever.applicationListenerBeans.add(listenerBeanName);
    }
    allListeners.add(listener);
    }
    }
    }
    catch (NoSuchBeanDefinitionException ex) {
    // Singleton listener instance (without backing bean definition) disappeared -
    // probably in the middle of the destruction phase
    }
    }
    }
    AnnotationAwareOrderComparator.sort(allListeners);
    return allListeners;
    }