• 【Spring框架】Spring监听器的源码分析


    目录

    一、Spring监听器模型

    二、源码分析

    2.1 initApplicationEventMulticaster():事件广播器的初始化

    2.1.1 Spring默认的事件广播器SimpleApplicationEventMulticaste

    2.2 registerListeners():注册事件监听器

    2.3 finishRefresh():完成refresh后发布相应的内置事件

    2.3.1 publishEvent(ApplicationEvent)方法:用于发布事件

    2.3.1.1 multicastEvent()方法

    2.3.1.1.1 getApplicationListeners(event,type)方法:获取所有监听event事件的监听器

    2.3.1.1.1.1 retrieveApplicationListeners()

    2.3.1.1.1.1.1 supportsEvent(listener,eventType) :判断当前监听器是否用来监听该事件

    2.3.1.1.2 invokeListener()方法:执行监听器的监听逻辑

    三、同步与异步

    3.1 默认同步通知

    3.2 异步通知设置

    3.2.1 第一种,直接自定义一个多播器,然后注册到容器中顶替掉Spring自动创建的多播器

    3.2.2 第二种,为现成的多播器设置设置一个线程池

    3.2.3 第三种,在监听器方法上加@Async注解


    一、Spring监听器模型

    前面我们讲了观察者模式的模型,它的模型主要是由观察者实体和主题实体构成,而Spring的监听器模式则结合了Spring本身的特征,也就是容器化。在Spring中,监听器实体全部放在ApplicationContext中,事件也是通过ApplicationContext来进行发布(ApplicationContext就相当于一个事件发布器),具体模型如下:

    我们不难看到,虽说是通过ApplicationContext发布的事件,但其并不是ApplicationContext自己进行事件的发布,而是引入了一个处理器—— EventMulticaster,直译就是事件多播器,用它来进行事件发布,它负责在大量的监听器中,针对每一个要广播的事件,找到事件对应的监听器,然后调用该监听器的响应方法,图中就是调用了监听器1、3、6。

    PS: 只有在某类事件第一次广播时,EventMulticaster才会去做遍历所有监听器的事,当它针对该类事件广播过一次后,就会把对应监听器保存起来了,最后会形成一个缓存Map,下一次就能直接找到这些监听器

    1. // 缓存
    2. final Map retrieverCache = new ConcurrentHashMap<>(64);

    二、源码分析

    在实际的面试过程中,关于Spring监听器的内容问的很少,一般只需要答出来下面的四个点即可,不用太深入的扣源码细节,明白它的设计思路就可以。

    Spring在ApplicationContext接口的抽象实现类AbstractApplicationContext中完成了事件体系的搭建。

    AbstractApplicationContext拥有一个applicationEventMulticaster成员变量,applicationEventMulticaster提供了容器监听器的注册表。

    当Spring容器启动的时候会调用refresh初始化容器,AbstractApplicationContext在refresh()这个容器启动方法中搭建了事件的基础设施。其中AbstractApplicationContext的refresh方法实现如下:

    AbstractApplicationContext.java

    1. public void refresh() throws BeansException, IllegalStateException {
    2. synchronized (this.startupShutdownMonitor) {
    3. // Prepare this context for refreshing.
    4. prepareRefresh();
    5. // Tell the subclass to refresh the internal bean factory.
    6. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    7. // Prepare the bean factory for use in this context.
    8. prepareBeanFactory(beanFactory);
    9. try {
    10. // Allows post-processing of the bean factory in context subclasses.
    11. postProcessBeanFactory(beanFactory);
    12. // Invoke factory processors registered as beans in the context.
    13. invokeBeanFactoryPostProcessors(beanFactory);
    14. // Register bean processors that intercept bean creation.
    15. registerBeanPostProcessors(beanFactory);
    16. // Initialize message source for this context.
    17. initMessageSource();
    18. // Initialize event multicaster for this context.
    19. // 事件多播器的初始化
    20. initApplicationEventMulticaster();
    21. // Initialize other special beans in specific context subclasses.
    22. onRefresh();
    23. // Check for listener beans and register them.
    24. // 将监听器注册到事件多播器中
    25. registerListeners();
    26. // Instantiate all remaining (non-lazy-init) singletons.
    27. finishBeanFactoryInitialization(beanFactory);
    28. // Last step: publish corresponding event.
    29. // 完成Spring容器的refresh后,会发布Spring的一些内置事件
    30. finishRefresh();
    31. }
    32. catch (BeansException ex) {
    33. if (logger.isWarnEnabled()) {
    34. logger.warn("Exception encountered during context initialization - " +
    35. "cancelling refresh attempt: " + ex);
    36. }
    37. // Destroy already created singletons to avoid dangling resources.
    38. destroyBeans();
    39. // Reset 'active' flag.
    40. cancelRefresh(ex);
    41. // Propagate exception to caller.
    42. throw ex;
    43. }
    44. finally {
    45. // Reset common introspection caches in Spring's core, since we
    46. // might not ever need metadata for singleton beans anymore...
    47. resetCommonCaches();
    48. }
    49. }
    50. }

    其中和监听器相关的方法就是initApplicationEventMulticaster()、registerListeners()和finishRefresh()

    2.1 initApplicationEventMulticaster():事件广播器的初始化

    AbstractApplicationContext.java

    1. protected void initApplicationEventMulticaster() {x
    2. // 获取ConfigurableListableBeanFactory工厂
    3. ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    4. // 首先看spring容器中是不是已经有名字为applicationEventMulticaster的对象,
    5. // 如果有就使用容器中已有的对象并赋值给AbstractApplicationContext类的成员属性applicationEventMulticaster
    6. if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
    7. this.applicationEventMulticaster =
    8. beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
    9. if (logger.isTraceEnabled()) {
    10. logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
    11. }
    12. }
    13. else {
    14. // 如果没有则新建一个SimpleApplicationEventMulticaster对象,并赋值给applicationEventMulticaster成员属性
    15. this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
    16. beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
    17. if (logger.isTraceEnabled()) {
    18. logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
    19. "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
    20. }
    21. }
    22. }

    用户可以在配置文件中为容器定义一个自定义的事件广播器,只要实现ApplicationEventMulticaster就可以了,Spring会通过反射的机制将其注册成容器的事件广播器,如果没有找到配置的外部事件广播器, Spring默认使用 SimpleApplicationEventMulticaster作为事件广播器。

    上面代码的逻辑最要是判断容器中是否有名字叫applicationEventMulticaster的事件广播器,如果有则使用已有的(这也算是Spring的一个扩展点,也就是如果我们创建了个名字是applicationEventMulticaster的事件广播器,那么就会使用我们自定义的),如果没有则新建默认的事件广播器SimpleApplicationEventMulticaster。这里会赋值给AbstractApplicationContext的applicationEventMulticaster成员变量。AbstractApplicationContext有一个名为parent的成员属性,是ApplicationContext类型,ApplicationContext也实现了ApplicationEventPublisher接口,所以它拥有事件相关的所有方法,但ApplicationContext只是一个代理,当我们调用applicationContext.publishEvent方法时,其实调用的就是applicationEventMulticaster 这个成员变量对象

    2.1.1 Spring默认的事件广播器SimpleApplicationEventMulticaste

    1. /**
    2. * SimpleApplicationEventMulticaster是Spring中的事件广播器类。
    3. * 它实现了事件的多播,即将事件通知给所有注册的监听器。
    4. * 这个类继承自AbstractApplicationEventMulticaster。
    5. */
    6. public class SimpleApplicationEventMulticaster extends AbstractApplicationEventMulticaster {
    7. /**
    8. * 事件多播器可以绑定线程池,以实现发布事件和执行监听器逻辑的异步执行
    9. *
    10. @Nullable
    11. private Executor taskExecutor;
    12. @Nullable
    13. private ErrorHandler errorHandler;
    14. /**
    15. * 创建一个新的SimpleApplicationEventMulticaster实例。
    16. */
    17. public SimpleApplicationEventMulticaster() {
    18. }
    19. /**
    20. * 使用给定的BeanFactory创建一个新的SimpleApplicationEventMulticaster实例。
    21. * @param beanFactory 给定的BeanFactory
    22. */
    23. public SimpleApplicationEventMulticaster(BeanFactory beanFactory) {
    24. setBeanFactory(beanFactory);
    25. }
    26. /**
    27. * 设置任务执行器,用于异步执行监听器的事件处理。
    28. * @param taskExecutor 任务执行器
    29. */
    30. public void setTaskExecutor(@Nullable Executor taskExecutor) {
    31. this.taskExecutor = taskExecutor;
    32. }
    33. /**
    34. * 获取当前的任务执行器。
    35. * @return 当前的任务执行器
    36. */
    37. @Nullable
    38. protected Executor getTaskExecutor() {
    39. return this.taskExecutor;
    40. }
    41. /**
    42. * 设置错误处理器,用于处理监听器抛出的异常。
    43. * @param errorHandler 错误处理器
    44. */
    45. public void setErrorHandler(@Nullable ErrorHandler errorHandler) {
    46. this.errorHandler = errorHandler;
    47. }
    48. /**
    49. * 获取当前的错误处理器。
    50. * @return 当前的错误处理器
    51. */
    52. @Nullable
    53. protected ErrorHandler getErrorHandler() {
    54. return this.errorHandler;
    55. }
    56. /**
    57. * 广播给定的应用事件到所有适当的监听器。
    58. * @param event 要广播的应用事件
    59. */
    60. @Override
    61. public void multicastEvent(ApplicationEvent event) {
    62. multicastEvent(event, resolveDefaultEventType(event));
    63. }
    64. /**
    65. * 广播给定的应用事件到所有适当的监听器,可以指定事件的类型。
    66. * @param event 要广播的应用事件
    67. * @param eventType 事件的类型,如果为null,则根据事件实例自动确定类型
    68. */
    69. @Override
    70. public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
    71. ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
    72. Executor executor = getTaskExecutor();
    73. for (ApplicationListener listener : getApplicationListeners(event, type)) {
    74. if (executor != null) {
    75. // 如果有任务执行器,使用任务执行器异步执行监听器的事件处理方法
    76. executor.execute(() -> invokeListener(listener, event));
    77. } else {
    78. // 否则同步执行监听器的事件处理方法
    79. invokeListener(listener, event);
    80. }
    81. }
    82. }
    83. /**
    84. * 解析默认的事件类型。
    85. * @param event 应用事件
    86. * @return 事件类型
    87. */
    88. private ResolvableType resolveDefaultEventType(ApplicationEvent event) {
    89. return ResolvableType.forInstance(event);
    90. }
    91. /**
    92. * 调用给定的监听器处理给定的事件。
    93. * @param listener 要调用的应用监听器
    94. * @param event 当前的事件
    95. * @since 4.1
    96. */
    97. protected void invokeListener(ApplicationListener listener, ApplicationEvent event) {
    98. ErrorHandler errorHandler = getErrorHandler();
    99. if (errorHandler != null) {
    100. try {
    101. // 调用监听器的事件处理方法,如果发生异常,交给错误处理器处理
    102. doInvokeListener(listener, event);
    103. } catch (Throwable err) {
    104. errorHandler.handleError(err);
    105. }
    106. } else {
    107. // 调用监听器的事件处理方法
    108. doInvokeListener(listener, event);
    109. }
    110. }
    111. /**
    112. * 调用监听器的事件处理方法,处理事件监听器可能抛出的异常。
    113. * @param listener 要调用的应用监听器
    114. * @param event 当前的事件
    115. */
    116. @SuppressWarnings({"rawtypes", "unchecked"})
    117. private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
    118. try {
    119. // 调用监听器的事件处理方法
    120. listener.onApplicationEvent(event);
    121. } catch (ClassCastException ex) {
    122. // 处理ClassCastException异常,可能是Lambda表达式定义的监听器,无法解析泛型事件类型
    123. String msg = ex.getMessage();
    124. if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
    125. // 忽略异常,记录调试日志
    126. Log logger = LogFactory.getLog(getClass());
    127. if (logger.isTraceEnabled()) {
    128. logger.trace("Non-matching event type for listener: " + listener, ex);
    129. }
    130. } else {
    131. // 抛出其他类型的ClassCastException异常
    132. throw ex;
    133. }
    134. }
    135. }
    136. /**
    137. * 判断ClassCastException异常的消息是否匹配给定的事件类。
    138. * @param classCastMessage ClassCastException异常的消息
    139. * @param eventClass 事件的类
    140. * @return 如果消息匹配事件类,返回true,否则返回false
    141. */
    142. private boolean matchesClassCastMessage(String classCastMessage, Class eventClass) {
    143. // 在Java 8中,消息以类名开头:"java.lang.String cannot be cast..."
    144. if (classCastMessage.startsWith(eventClass.getName())) {
    145. return true;
    146. }
    147. // 在Java 11中,消息以"class ..."开头,即Class.toString()的返回值
    148. if (classCastMessage.startsWith(eventClass.toString())) {
    149. return true;
    150. }
    151. // 在Java 9中,消息包含模块名:"java.base/java.lang.String cannot be cast..."
    152. int moduleSeparatorIndex = classCastMessage.indexOf('/');
    153. if (moduleSeparatorIndex != -1 && classCastMessage.startsWith(eventClass.getName(), moduleSeparatorIndex + 1)) {
    154. return true;
    155. }
    156. // 假设是不相关的ClassCastException异常,返回false
    157. return false;
    158. }
    159. }

    遍历注册的每个监听器,并启动来调用每个监听器的onApplicationEvent方法。由于 SimpleApplicationEventMulticaster的taskExecutor的实现类是SyncTaskExecutor,因此,该事件监听器对事件的处理是同步进行的。

    2.2 registerListeners():注册事件监听器

    通过registerListeners()方法将事件监听器注册到事件多播器中。在上一步事件多播器已经创建出来了,而事件监听器此时也已经在Spring容器中了,具体是什么时候创建的事件监听器我们后面会讲到。

    AbstractApplicationContext.java

    1. protected void registerListeners() {
    2. // Register statically specified listeners first.
    3. // 获取容器中所有的监听器对象
    4. // 这个时候正常流程是不会有监听器的,除非手动调用addApplicationListeners()
    5. for (ApplicationListener listener : getApplicationListeners()) {
    6. // 把监听器挨个的注册到我们的多播器上去
    7. getApplicationEventMulticaster().addApplicationListener(listener);
    8. }
    9. // 这里是为了防止懒加载监听器漏网,因为懒加载的Bean只有在调用的时候才会创建,所以容器中不会用这个Bean,只能从Bean定义中去获取
    10. // 获取bean定义中的监听器对象 接口方式实现的监听就是在这里注册到多播器里面的
    11. // Do not initialize FactoryBeans here: We need to leave all regular beans
    12. // uninitialized to let post-processors apply to them!
    13. String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    14. // 把监听器的名称注册到我们的事件多播器上
    15. for (String listenerBeanName : listenerBeanNames) {
    16. getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
    17. }
    18. // 在这里获取早期事件
    19. // Publish early application events now that we finally have a multicaster...
    20. Set earlyEventsToProcess = this.earlyApplicationEvents;
    21. // 在这里赋null。 也就是在此之后都就没有早期事件了
    22. this.earlyApplicationEvents = null;
    23. if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
    24. // 通过多播器进行播发早期事件
    25. for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
    26. getApplicationEventMulticaster().multicastEvent(earlyEvent);
    27. }
    28. }
    29. }

    Spring根据反射机制,使用ListableBeanFactory的getBeansOfType方法,从BeanDefinitionRegistry中找出所有实现 org.springframework.context.ApplicationListener的Bean,将它们注册为容器的事件监听器,实际的操作就是将其添加到事件广播器所提供的监听器注册表中。

    2.3 finishRefresh():完成refresh后发布相应的内置事件

    AbstractApplicationContext.java

    1. protected void finishRefresh() {
    2. // Clear context-level resource caches (such as ASM metadata from scanning).
    3. clearResourceCaches();
    4. // Initialize lifecycle processor for this context.
    5. initLifecycleProcessor();
    6. // Propagate refresh to lifecycle processor first.
    7. getLifecycleProcessor().onRefresh();
    8. // Publish the final event.
    9. // 发布容器启动完毕事件ContextRefreshedEvent
    10. publishEvent(new ContextRefreshedEvent(this));
    11. // Participate in LiveBeansView MBean, if active.
    12. LiveBeansView.registerApplicationContext(this);
    13. }

    2.3.1 publishEvent(ApplicationEvent)方法:用于发布事件

    事件发布的关键方法流程如下:

    1. org.springframework.context.support.AbstractApplicationContext#publishEvent //发布事件
    2. -->org.springframework.context.support.AbstractApplicationContext#publishEvent //发布事件
    3. -->org.springframework.context.event.SimpleApplicationEventMulticaster#multicastEvent //发布事件
    4. -->org.springframework.context.event.AbstractApplicationEventMulticaster#getApplicationListeners //找到感兴趣的事件
    5. -->org.springframework.context.event.AbstractApplicationEventMulticaster#retrieveApplicationListeners//找到感兴趣的事件
    6. -->org.springframework.context.event.AbstractApplicationEventMulticaster#supportsEvent //判断是否是感兴趣的事件

    我们先跟着 finishRefresh()方法进入publishEvent(ApplicationEvent )方法,这个方法就是用于发布事件的(传入方法的就是要发布的事件),源码如下:

    AbstractApplicationContext.java

    1. public void publishEvent(ApplicationEvent event) {
    2. publishEvent(event, null);
    3. }
    4. protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
    5. Assert.notNull(event, "Event must not be null");
    6. // Decorate event as an ApplicationEvent if necessary
    7. ApplicationEvent applicationEvent;
    8. if (event instanceof ApplicationEvent) {
    9. applicationEvent = (ApplicationEvent) event;
    10. }
    11. else {
    12. applicationEvent = new PayloadApplicationEvent<>(this, event);
    13. if (eventType == null) {
    14. eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
    15. }
    16. }
    17. // Multicast right now if possible - or lazily once the multicaster is initialized
    18. if (this.earlyApplicationEvents != null) {
    19. this.earlyApplicationEvents.add(applicationEvent);
    20. }
    21. else {
    22. getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
    23. }
    24. // Publish event via parent context as well...
    25. if (this.parent != null) {
    26. if (this.parent instanceof AbstractApplicationContext) {
    27. ((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
    28. }
    29. else {
    30. this.parent.publishEvent(event);
    31. }
    32. }
    33. }

    在AbstractApplicationContext的publishEvent方法中, Spring委托ApplicationEventMulticaster将事件通知给所有的事件监听器。

    2.3.1.1 multicastEvent()方法

    所有的真正发布动作都是由multicastEvent()完成的。multicastEvent 方法很简单,调用getApplicationListeners方法找到所有对当前事件感兴趣的监听器,如果存在线程池,则异步执行监听逻辑,否则就同步执行。默认情况下执行事件的逻辑是跟发布事件是同一个线程。

    SimpleApplicationEventMulticaster.java

    1. @Override
    2. public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
    3. ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
    4. // 获取当前事件多播器的线程池。事件多播器类中有一个线程池成员属性,用来记录和事件多播器绑定在一起的线程池
    5. Executor executor = getTaskExecutor();
    6. // getApplicationListeners 方法找到所有的对当前事件感兴趣的监听器
    7. for (ApplicationListener listener : getApplicationListeners(event, type)) {
    8. if (executor != null) {
    9. // 如果有配置线程池,则使用线程池执行监听逻辑,也就是触发监听器对事件发生后的执行逻辑
    10. executor.execute(() -> invokeListener(listener, event));
    11. }
    12. else {
    13. // 没有线程池,就用发布事件的线程去执行监控器在事件发生后触发的逻辑
    14. invokeListener(listener, event);
    15. }
    16. }
    17. }

    整个代码逻辑很简单,就是判断是否有Executor,如果有的话异步加载invokeListener方法,没有的话同步调用invokeListener方法

    2.3.1.1.1 getApplicationListeners(eventtype)方法:获取所有监听event事件的监听器

    getApplicationListeners方法,主要是判断缓存中有没有已经缓存好的事件,有则直接返回,没有则调用方法retrieveApplicationListeners检索监听器。

    AbstractApplicationEventMulticaster.java

    1. protected Collection> getApplicationListeners(
    2. ApplicationEvent event, ResolvableType eventType) {
    3. Object source = event.getSource();
    4. Class sourceType = (source != null ? source.getClass() : null);
    5. // 生成一个用于表示当前这个事件event的缓存key
    6. ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);
    7. // Potential new retriever to populate
    8. CachedListenerRetriever newRetriever = null;
    9. // Quick check for existing entry on ConcurrentHashMap
    10. // 判断缓存中是不是已经缓存了对该事件感兴趣的监听器
    11. CachedListenerRetriever existingRetriever = this.retrieverCache.get(cacheKey);
    12. // 没有的话,新建一个键值对,并放到map中。这个键值对就用来存放监听这个事件的所有监听器
    13. if (existingRetriever == null) {
    14. // Caching a new ListenerRetriever if possible
    15. if (this.beanClassLoader == null ||
    16. (ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
    17. (sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
    18. newRetriever = new CachedListenerRetriever();
    19. existingRetriever = this.retrieverCache.putIfAbsent(cacheKey, newRetriever);
    20. if (existingRetriever != null) {
    21. newRetriever = null; // no need to populate it in retrieveApplicationListeners
    22. }
    23. }
    24. }
    25. // 缓存中有,直接从缓存中获取该事件的监听器并返回
    26. if (existingRetriever != null) {
    27. Collection> result = existingRetriever.getApplicationListeners();
    28. if (result != null) {
    29. return result;
    30. }
    31. // If result is null, the existing retriever is not fully populated yet by another thread.
    32. // Proceed like caching wasn't possible for this current local attempt.
    33. }
    34. // 缓存中没有,就调用retrieveApplicationListeners方法查找事件ApplicationEvent。
    35. return retrieveApplicationListeners(eventType, sourceType, newRetriever);
    36. }

    2.3.1.1.1.1 retrieveApplicationListeners()

    this.defaultRetriever.applicationListeners和this.defaultRetriever.applicationListenerBeans存放了所有的Spring事件,最终会调用supportsEvent方法过滤出对当期事件感兴趣的监听器。

    AbstractApplicationEventMulticaster.java

    1. private Collection> retrieveApplicationListeners(
    2. ResolvableType eventType, @Nullable Class sourceType, @Nullable CachedListenerRetriever retriever) {
    3. List> allListeners = new ArrayList<>();
    4. Set> filteredListeners = (retriever != null ? new LinkedHashSet<>() : null);
    5. Set filteredListenerBeans = (retriever != null ? new LinkedHashSet<>() : null);
    6. Set> listeners;
    7. Set listenerBeans;
    8. synchronized (this.defaultRetriever) {
    9. // this.defaultRetriever.applicationListeners存放了所有的applicationListeners
    10. listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
    11. // this.defaultRetriever.applicationListenerBeans存放的是PayloadApplicationEvent事件
    12. listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
    13. }
    14. // Add programmatically registered listeners, including ones coming
    15. // from ApplicationListenerDetector (singleton beans and inner beans).
    16. for (ApplicationListener listener : listeners) {
    17. // 判断遍历到的监听器是否对当前事件感兴趣
    18. if (supportsEvent(listener, eventType, sourceType)) {
    19. if (retriever != null) {
    20. filteredListeners.add(listener);
    21. }
    22. allListeners.add(listener);
    23. }
    24. }
    25. ...............省略
    26. }

    事件监听器是什么时候创建并加入到defaultRetriever.applicationListeners中的呢?

    我们将监听器注册到事件多播器就会引出一个问题,这里以this.defaultRetriever.applicationListeners为例,this.defaultRetriever.applicationListeners中所有的监听器是什么时候加入进去的呢?

    this.defaultRetriever.applicationListeners中包含两种监听器:

    1. 实现了ApplicationListener接口。
    2. @EventListener 注解

    1 查找实现了ApplicationListener接口的监听器

    这里关键调用ApplicationListenerDetector的postProcessMergedBeanDefinition方法和postProcessAfterInitialization方法(这个方法也就是常说的bean初始化后)。这两个都是BeanPostProcessor的子类提供的方法。

    1.1 postProcessMergedBeanDefinition()方法

    postProcessMergedBeanDefinition方法的作用很简单,就是判断这个Java类是否实现了ApplicationListener接口,是的话就是一个监听器。然后将这个监听器加入到singletonNames的记录单例Bean的Map中。

    ApplicationListenerDetector.java

    1. @Override
    2. public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) {
    3. if (ApplicationListener.class.isAssignableFrom(beanType)) {
    4. this.singletonNames.put(beanName, beanDefinition.isSingleton());
    5. }
    6. }

    1.2 postProcessAfterInitialization()方法

    postProcessAfterInitialization方法也很简单,从singletonNames拿到所有的单例ApplicationListener。

    并调用this.applicationContext.addApplicationListener((ApplicationListener) bean)方法,前面提到过applicationContext也实现ApplicationEventPublisher接口,拥有事件发布的所有方法,但实际执行的是成员变量applicationEventMulticaster对象。最终就将监听器添加到了this.defaultRetriever.applicationListeners中。

    ApplicationListenerDetector.java

    1. @Override
    2. public Object postProcessAfterInitialization(Object bean, String beanName) {
    3. if (bean instanceof ApplicationListener) {
    4. // potentially not detected as a listener by getBeanNamesForType retrieval
    5. Boolean flag = this.singletonNames.get(beanName);
    6. if (Boolean.TRUE.equals(flag)) {
    7. // 拿到所有的单例ApplicationListener并添加到defaultRetriever.applicationListeners
    8. this.applicationContext.addApplicationListener((ApplicationListener) bean);
    9. }
    10. else if (Boolean.FALSE.equals(flag)) {
    11. if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
    12. // inner bean with other scope - can't reliably process events
    13. logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
    14. "but is not reachable for event multicasting by its containing ApplicationContext " +
    15. "because it does not have singleton scope. Only top-level listener beans are allowed " +
    16. "to be of non-singleton scope.");
    17. }
    18. this.singletonNames.remove(beanName);
    19. }
    20. }
    21. return bean;
    22. }

    this.applicationContext.addApplicationListener((ApplicationListener) bean)最终执行的方法其实是AbstractApplicationEventMulticaster类的addApplicationListener方法。

    其会最终调用this.defaultRetriever.applicationListeners.add(listener)方法。

    AbstractApplicationContext.java

    1. public void addApplicationListener(ApplicationListener listener) {
    2. Assert.notNull(listener, "ApplicationListener must not be null");
    3. if (this.applicationEventMulticaster != null) {
    4. // 调用applicationEventMulticaster.addApplicationListener,来进行监听器的注册
    5. this.applicationEventMulticaster.addApplicationListener(listener);
    6. }
    7. this.applicationListeners.add(listener);
    8. }

     

    AbstractApplicationEventMulticaster.java

    1. // 将监听器加入到defaultRetriever.applicationListeners中
    2. public void addApplicationListener(ApplicationListener listener) {
    3. synchronized (this.defaultRetriever) {
    4. // Explicitly remove target for a proxy, if registered already,
    5. // in order to avoid double invocations of the same listener.
    6. Object singletonTarget = AopProxyUtils.getSingletonTarget(listener);
    7. if (singletonTarget instanceof ApplicationListener) {
    8. this.defaultRetriever.applicationListeners.remove(singletonTarget);
    9. }
    10. // 加入监听器
    11. this.defaultRetriever.applicationListeners.add(listener);
    12. this.retrieverCache.clear();
    13. }
    14. }

    2 查找加了@EventListener 注解的监听器

    我们知道,在生成流程中,会对每个Bean都使用PostProcessor来进行加工。查找加了@EventListener 注解的监听器主要是通过EventListenerMethodProcessor类,由方法名可以看到,这也是一个bean的后置处理器,这个类会在Bean实例化后进行一系列操作。EventListenerMethodProcessor类实现了SmartInitializingSingleton接口,查找@EventListener注解的监听器的功能是通过调用EventListenerMethodProcessor类实现的SmartInitializingSingleton接口的afterSingletonsInstantiated方法来完成,这个方法会在容器初始化完成之后执行。

    查找的方式也很简单粗暴,直接调用beanFactory.getBeanNamesForType(Object.class)方法获取Spring容器中的所有对象,然后再去判断这个Bean的方法是否加了@EventListener注解。

    EventListenerMethodProcessor.java

    1. @Override
    2. public void afterSingletonsInstantiated() {
    3. ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    4. Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
    5. // 获取Spring容器中的所有Bean的name
    6. String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
    7. for (String beanName : beanNames) {
    8. ................省略
    9. try {
    10. // 查找该Bean的方法中是加了@EventListener注解
    11. processBean(beanName, type);
    12. }
    13. catch (Throwable ex) {
    14. throw new BeanInitializationException("Failed to process @EventListener " +
    15. "annotation on bean with name '" + beanName + "'", ex);
    16. }
    17. }
    18. }

    然后调用processBean()方法,遍历对象中的方法是否有加了EventListener注解的方法。有则调用DefaultEventListenerFactory的createApplicationListener方法创建一个是配置器对象ApplicationListenerMethodAdapter,构造器参数是方法对象,bean类对象,bean对象。最后调用context.addApplicationListener方法把监听器放到this.defaultRetriever.applicationListeners中

    EventListenerMethodProcessor.java

    1. private void processBean(final String beanName, final Class targetType) {
    2. if (!this.nonAnnotatedClasses.contains(targetType) &&
    3. AnnotationUtils.isCandidateClass(targetType, EventListener.class) &&
    4. !isSpringContainerClass(targetType)) {
    5. Map annotatedMethods = null;
    6. try {
    7. // 遍历对象中的方法是否有加了EventListener注解的方法,将它们都存到annotatedMethods中
    8. annotatedMethods = MethodIntrospector.selectMethods(targetType,
    9. (MethodIntrospector.MetadataLookup) method ->
    10. AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));
    11. }
    12. ..............省略..................
    13. // Non-empty set of methods
    14. ConfigurableApplicationContext context = this.applicationContext;
    15. Assert.state(context != null, "No ApplicationContext set");
    16. List factories = this.eventListenerFactories;
    17. Assert.state(factories != null, "EventListenerFactory List not initialized");
    18. // 遍历加了EventListener注解的方法
    19. for (Method method : annotatedMethods.keySet()) {
    20. // 遍历监听器工厂,这类工厂是专门用来创建监听器的,我们以起作用的是DefaultEventListenerFactory默认工厂为例
    21. for (EventListenerFactory factory : factories) {
    22. // DefaultEventListenerFactory是永远返回true的
    23. if (factory.supportsMethod(method)) {
    24. Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
    25. // 调用DefaultEventListenerFactory的createApplicationListener的方法,利用该Bean名、Bean类型、方法来创建监听器(其实是适配器类)
    26. ApplicationListener applicationListener =
    27. factory.createApplicationListener(beanName, targetType, methodToUse);
    28. if (applicationListener instanceof ApplicationListenerMethodAdapter) {
    29. ((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
    30. }
    31. // 把监听器添加到this.defaultRetriever.applicationListeners容器中
    32. context.addApplicationListener(applicationListener);
    33. break;
    34. }
    35. }
    36. }
    37. }
    38. }

                  

    以DefaultEventListenerFactory为例,用默认工厂类来创建监听器(适配器类)ApplicationListenerMethodAdapter。

    1. public class DefaultEventListenerFactory implements EventListenerFactory, Ordered {
    2. // 省略其余代码
    3. @Override
    4. public ApplicationListener createApplicationListener(String beanName, Class type, Method method) {
    5. // 可以看到,每次都是返回一个新对象,每一个加了@EventListener注解的方法就会对应生成一个监听器。例如同一个类中有两个加了@EventListener注解的方法,那么就会生成两个监听器对象
    6. return new ApplicationListenerMethodAdapter(beanName, type, method);
    7. }
    8. }

    2.3.1.1.1.1.1 supportsEvent(listenereventType) 判断当前监听器是否用来监听该事件

    Spring发布事件之后,所有注册的事件监听器,都会收到该事件,因此,事件监听器在处理事件时,需要先判断该事件是否是自己关心的。

    所以梳理完了监听器的查找过程,下面我们继续来看看Spring监听器是如何查找感兴趣的事件的。

    判断监听器是否用来监听指定事件的逻辑代码还是比较清晰的,先判断当前监听器是不是继承了GenericApplicationListener。如果不是继承GenericApplicationListener的监听器,将会被GenericApplicationListenerAdapter适配器再次包装。

    GenericApplicationListener 实现SmartApplicationListener,SmartApplicationListener实现了ApplicationListener接口。但是GenericApplicationListener 的作用是实现了SmartApplicationListener接口的两个方法supportsEventType()和supportsSourceType()。

    这两个方法的作用是:

    • supportsEventType(eventType):检查监听器是否支持指定的事件类型。在事件广播过程中,系统会遍历所有的监听器,但并非所有的监听器都对所有类型的事件感兴趣。这个方法用来检查给定的监听器是否对特定类型的事件感兴趣。
    • supportsSourceType(sourceType):检查监听器是否支持指定的事件源类型。有时候,监听器只对特定类型的事件源感兴趣,这个方法用来确定监听器是否支持特定类型的事件源。

    这两个方法通常在事件广播器内部使用,用于确定哪些监听器应该接收特定类型的事件。

    1. protected boolean supportsEvent(
    2. ApplicationListener listener, ResolvableType eventType, @Nullable Class sourceType) {
    3. // 如果listener监听器没有继承GenericApplicationListener,就再用GenericApplicationListener将其封装一层
    4. GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
    5. (GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
    6. // 判断监听器是否支持指定的事件类型和事件源类型
    7. return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
    8. }

    我们来看一下GenericApplicationListener接口源码:

    1. public interface GenericApplicationListener extends SmartApplicationListener {
    2. @Override
    3. default boolean supportsEventType(Class eventType) {
    4. return supportsEventType(ResolvableType.forClass(eventType));
    5. }
    6. boolean supportsEventType(ResolvableType eventType);
    7. }

    从接口方法可以看到GenericApplicationListener 的子类如果没重写supportsEventType方法的话,就表示对所有的事件感兴趣。通过supportsEventType方法,我们可以自定义我们感兴趣的事件。

    而相对于GenericApplicationListener接口,适配类GenericApplicationListenerAdapter实现了supportsEventType方法,它的实现就是判断当前监听器的泛型是不是当前事件或者是当前事件的子类(this.declaredEventType.isAssignableFrom(eventType))。

    GenericApplicationListenerAdapter.java

    1. public boolean supportsEventType(ResolvableType eventType) {
    2. if (this.delegate instanceof GenericApplicationListener) {
    3. return ((GenericApplicationListener) this.delegate).supportsEventType(eventType);
    4. }
    5. else if (this.delegate instanceof SmartApplicationListener) {
    6. Classextends ApplicationEvent> eventClass = (Classextends ApplicationEvent>) eventType.resolve();
    7. return (eventClass != null && ((SmartApplicationListener) this.delegate).supportsEventType(eventClass));
    8. }
    9. // 默认实现
    10. else {
    11. return (this.declaredEventType == null || this.declaredEventType.isAssignableFrom(eventType));
    12. }
    13. }

    2.3.1.1.2 invokeListener()方法:执行监听器的监听逻辑

    SimpleApplicationEventMulticaster.java

    1. protected void invokeListener(ApplicationListener listener, ApplicationEvent event) {
    2. ErrorHandler errorHandler = getErrorHandler();
    3. if (errorHandler != null) {
    4. try {
    5. doInvokeListener(listener, event);
    6. }
    7. catch (Throwable err) {
    8. errorHandler.handleError(err);
    9. }
    10. }
    11. else {
    12. doInvokeListener(listener, event);
    13. }
    14. }
    15. private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
    16. try {
    17. listener.onApplicationEvent(event);
    18. }
    19. catch (ClassCastException ex) {
    20. String msg = ex.getMessage();
    21. if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
    22. // Possibly a lambda-defined listener which we could not resolve the generic event type for
    23. // -> let's suppress the exception and just log a debug message.
    24. Log logger = LogFactory.getLog(getClass());
    25. if (logger.isTraceEnabled()) {
    26. logger.trace("Non-matching event type for listener: " + listener, ex);
    27. }
    28. }
    29. else {
    30. throw ex;
    31. }
    32. }
    33. }

    在doInvokeListener()方法中,终于看到了熟悉的onApplicationEvent方法,这个方法就是我们在自定义监听器时重写的方法,也正是在这里监听器调用了我们自己定义的onApplicationEvent(),实现了当监听器监听到事件发生后,就执行一些自定义的功能。

    三、同步与异步

    通过模型,我们不难看出,事件的发布其实由业务线程来发起,那么那些监听器的触发呢,是仍由业务线程一个个同步地去通知监听器,还是有专门的线程接手,收到事件后,再转手通知监听器们?

    3.1 默认同步通知

    其实,因为Spring默认的多播器没有设置执行器,所以默认采用的是第一种情况,即哪个线程发起的事件,则由哪个线程去通知监听器们,关键代码上面已经讲过了,如下所示:

    1. // SimpleApplicationEventMulticaster.java
    2. public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
    3. ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
    4. Executor executor = getTaskExecutor();
    5. for (ApplicationListener listener : getApplicationListeners(event, type)) {
    6. if (executor != null) {
    7. executor.execute(() -> invokeListener(listener, event));
    8. }
    9. else {
    10. // 默认走此分支,由发出事件的线程来执行
    11. invokeListener(listener, event);
    12. }
    13. }
    14. }
    15. @Nullable
    16. protected Executor getTaskExecutor() {
    17. return this.taskExecutor;
    18. }

    我们可以看到,对于每个监听器的调用是同步还是异步,取决于多播器内部是否含有一个执行器,如果有则交给执行器去执行,如果没有,只能让来源线程一一去通知了。

    3.2 异步通知设置

    两种方式,一种是在多播器创建时内置一个线程池,使多播器能够调用自身的线程池去执行事件传播。另一种是不在多播器上做文章,而是在每个监视器的响应方法上标注异步@Async注解,毫无疑问,第一种才是正道,我们来看看如何做到。其实有多种方法,我们这里说几种。

    3.2.1 第一种,直接自定义一个多播器,然后注册到容器中顶替掉Spring自动创建的多播器

    1. @Configuration
    2. public class EventConfig {
    3. @Bean("taskExecutor")
    4. public Executor getExecutor() {
    5. ThreadPoolExecutor executor = new ThreadPoolExecutor(10,
    6. 15,
    7. 60,
    8. TimeUnit.SECONDS,
    9. new ArrayBlockingQueue(2000));
    10. return executor;
    11. }
    12. // 这其实就是spring-boot自动配置的雏形,所谓的自动配置其实就是通过各种配置类,顶替原有的简单配置
    13. @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    14. public ApplicationEventMulticaster initEventMulticaster(@Qualifier("taskExecutor") Executor taskExecutor) {
    15. SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
    16. simpleApplicationEventMulticaster.setTaskExecutor(taskExecutor);
    17. return simpleApplicationEventMulticaster;
    18. }
    19. }

    3.2.2 第二种,为现成的多播器设置设置一个线程池

    1. @Component
    2. public class WindowsCheck implements ApplicationContextAware {
    3. @Override
    4. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    5. SimpleApplicationEventMulticaster caster = (SimpleApplicationEventMulticaster)applicationContext
    6. .getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME);
    7. ThreadPoolExecutor executor = new ThreadPoolExecutor(10,
    8. 15,
    9. 60,
    10. TimeUnit.SECONDS,
    11. new ArrayBlockingQueue(2000));
    12. caster.setTaskExecutor(executor);
    13. }
    14. }

    当然,这里推荐第一种,第二种方法会在Spring启动初期的一些事件上,仍采用同步的方式。直至被注入一个线程池后,其才能使用线程池来响应事件。而第一种方法则是官方暴露的位置,让我们去构建自己的多播器。

    3.2.3 第三种,在监听器方法上加@Async注解

    1. @Component
    2. public class OrderEventListener {
    3. @Async
    4. @EventListener(OrderEvent.class)
    5. public void onApplicationEvent(OrderEvent event) {
    6. if(event.getName().equals("减库存")){
    7. System.out.println("减库存.......");
    8. }
    9. }
    10. }

    方法增加@Async注解,表示该方法为异步方法。


    相关文章:【Spring框架】Spring监听器的简介和基本使用

  • 相关阅读:
    Java面试题08
    软件测试的风险主要体现在哪里
    MyBatisPlus又在搞事了,发布神器,一个依赖轻松搞定权限问题?
    25-多线程
    java-php-python-会议查询系统计算机毕业设计
    ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
    我用ChatGPT写2023高考语文作文(一):全国甲卷
    TensorFlow 2.9的零零碎碎(二)-构建模型
    133道Java面试题及答案(面试必看)
    含分布式电源的配电网可靠性评估(matlab代码)
  • 原文地址:https://blog.csdn.net/cy973071263/article/details/133839965