Spring中的EventListenerMethodProcessor组件详解

 更新时间:2023年12月25日 10:02:52   作者:安迪源文  
这篇文章主要介绍了Spring中的EventListenerMethodProcessor组件详解,EventListenerMethodProcessor 是 Spring 事件机制中非常重要的一个组件,它管理了一组EventListenerFactory组件,用来将应用中每个使用@EventListener注解定义的事件监听,需要的朋友可以参考下

EventListenerMethodProcessor组件

1. 概述

EventListenerMethodProcessor 是 Spring 事件机制中非常重要的一个组件。它管理了一组EventListenerFactory组件,用来将应用中每个使用@EventListener注解定义的事件监听方法变成一个ApplicationListener实例注册到容器。换句话讲,框架开发者,或者应用开发者使用注解@EventListener定义的事件处理方法,如果没有EventListenerMethodProcessor的发现和注册,是不会被容器看到和使用的。

EventListenerMethodProcessor实现了如下三个接口 :

  • ApplicationContextAware
  • BeanFactoryPostProcessor
  • SmartInitializingSingleton

通过实现接口ApplicationContextAware,容器会将当前应用上下文ApplicationContext告诉EventListenerMethodProcessor,这是EventListenerMethodProcessor用于检测发现@EventListener注解方法的来源,生成的ApplicationListener也放到该应用上下文。

通过实现接口BeanFactoryPostProcessor,EventListenerMethodProcessor变成了一个BeanFactory的后置处理器,也就是说,在容器启动过程中的后置处理阶段,启动过程会调用EventListenerMethodProcessor的方法postProcessBeanFactory。在这个方法中,EventListenerMethodProcessor会找到容器中所有类型为EventListenerFactory的bean,最终@EventListener注解方法的检测发现,以及ApplicationListener实例的生成和注册,靠的是这些EventListenerFactory组件。

而通过实现接口SmartInitializingSingleton,在容器启动过程中所有单例bean创建阶段(此阶段完成前,这些bean并不会供外部使用)的末尾,EventListenerMethodProcessor的方法afterSingletonsInstantiated会被调用。在这里,EventListenerMethodProcessor会便利容器中所有的bean,进行@EventListener注解方法的检测发现,以及ApplicationListener实例的生成和注册。

2. 源代码

代码版本 : Spring Context 5.2.0.RELEASE

package org.springframework.context.event;
//... 省略 import
/**
 * Registers {@link EventListener} methods as individual {@link ApplicationListener} instances.
 * Implements {@link BeanFactoryPostProcessor} (as of 5.1) primarily for early retrieval,
 * avoiding AOP checks for this processor bean and its {@link EventListenerFactory} delegates.
 *
 * @author Stephane Nicoll
 * @author Juergen Hoeller
 * @since 4.2
 * @see EventListenerFactory
 * @see DefaultEventListenerFactory
 */
public class EventListenerMethodProcessor
		implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor {
	protected final Log logger = LogFactory.getLog(getClass());
    // 用于记录检测发现`@EventListener`注解方法,生成和注册`ApplicationListener`实例的应用上下文
	@Nullable
	private ConfigurableApplicationContext applicationContext;
   // 记录当前 BeanFactory, 实际上这个变量可用可不用,因为通过 applicationContext 也可以找到
   // 当前 BeanFactory
	@Nullable
	private ConfigurableListableBeanFactory beanFactory;
   // 记录从容器中找到的所有 EventListenerFactory
	@Nullable
	private List<EventListenerFactory> eventListenerFactories;
	private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator();
    // 缓存机制,记住那些根本任何方法上没有使用注解 @EventListener 的类,避免处理过程中二次处理 
	private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext,
				"ApplicationContext does not implement ConfigurableApplicationContext");
		this.applicationContext = (ConfigurableApplicationContext) applicationContext;
	}
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		this.beanFactory = beanFactory;
       // 从容器中找到所有的  EventListenerFactory 组件
       // 常见的一些 EventListenerFactory :
       // TransactionalEventListenerFactory --
       // 用于支持使用 @TransactionalEventListener 注解的事件监听器, @TransactionalEventListener 是一种特殊的
       // @EventListener,它定义的事件监听器应用于事务提交或者回滚的某些特殊时机,
       // 由 ProxyTransactionManagementConfiguration 注册到容器
       // 注册到容器
       // DefaultEventListenerFactory -- 系统缺省, 最低优先级,如果其他 EventListenerFactory 都不支持的时候使用
		Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
		List<EventListenerFactory> factories = new ArrayList<>(beans.values());
		AnnotationAwareOrderComparator.sort(factories);
		this.eventListenerFactories = factories;
	}
	@Override
	public void afterSingletonsInstantiated() {
		ConfigurableListableBeanFactory beanFactory = this.beanFactory;
		Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
       // 这里获取容器中所有bean组件的名称, 
		String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
		for (String beanName : beanNames) {
          // 遍历每个bean组件,检测其中`@EventListener`注解方法,生成和注册`ApplicationListener`实例
			if (!ScopedProxyUtils.isScopedTarget(beanName)) {
				Class<?> type = null;
				try {
					type = AutoProxyUtils.determineTargetClass(beanFactory, beanName);
				}
				catch (Throwable ex) {
					// An unresolvable bean type, probably from a lazy bean - let's ignore it.
					if (logger.isDebugEnabled()) {
						logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
					}
				}
				if (type != null) {
					if (ScopedObject.class.isAssignableFrom(type)) {
						try {
							Class<?> targetClass = AutoProxyUtils.determineTargetClass(
									beanFactory, ScopedProxyUtils.getTargetBeanName(beanName));
							if (targetClass != null) {
								type = targetClass;
							}
						}
						catch (Throwable ex) {
							// An invalid scoped proxy arrangement - let's ignore it.
							if (logger.isDebugEnabled()) {
								logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex);
							}
						}
					}
					try {
                     // 注意这一行,针对一个bean的真正的`@EventListener`注解方法检测,
                     // `ApplicationListener`实例生成注册发生在这里
						processBean(beanName, type);
					}
					catch (Throwable ex) {
						throw new BeanInitializationException("Failed to process @EventListener " +
								"annotation on bean with name '" + beanName + "'", ex);
					}
				}
			}
		}
	}
   // 该方法拿到某个bean的名称和它的目标类,在这个范围上检测`@EventListener`注解方法,
   // 生成和注册`ApplicationListener`实例
	private void processBean(final String beanName, final Class<?> targetType) {
		if (!this.nonAnnotatedClasses.contains(targetType) &&
				AnnotationUtils.isCandidateClass(targetType, EventListener.class) &&
				!isSpringContainerClass(targetType)) {
			Map<Method, EventListener> annotatedMethods = null;
			try {
             // *** 注意这里, 这里检测当前类targetType上使用了注解 @EventListener 的方法
				annotatedMethods = MethodIntrospector.selectMethods(targetType,
						(MethodIntrospector.MetadataLookup<EventListener>) method ->
								AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));
			}
			catch (Throwable ex) {
				// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
				if (logger.isDebugEnabled()) {
					logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
				}
			}
			if (CollectionUtils.isEmpty(annotatedMethods)) {
              // 如果当前类 targetType 中没有任何使用了 注解 @EventListener 的方法,则将该类保存到
              // 缓存 nonAnnotatedClasses, 从而避免当前处理方法重入该类,其目的应该是为了提高效率,             
				this.nonAnnotatedClasses.add(targetType);
				if (logger.isTraceEnabled()) {
					logger.trace("No @EventListener annotations found on bean class: " + targetType.getName());
				}
			}
			else {
              // 发现当前类 targetType 中有些方法使用了注解 @EventListener,现在根据这些方法上的信息
              // 对应地创建和注册ApplicationListener实例
				// Non-empty set of methods
				ConfigurableApplicationContext context = this.applicationContext;
				Assert.state(context != null, "No ApplicationContext set");
              // 注意,这里使用到了  this.eventListenerFactories, 这些 EventListenerFactory 是在 
              // 该类 postProcessBeanFactory 方法调用时被记录的
				List<EventListenerFactory> factories = this.eventListenerFactories;
				Assert.state(factories != null, "EventListenerFactory List not initialized");
				for (Method method : annotatedMethods.keySet()) {
					for (EventListenerFactory factory : factories) {
						if (factory.supportsMethod(method)) {
                        // 如果当前 EventListenerFactory factory 支持处理该 @EventListener 注解的方法,
                        // 则使用它创建 ApplicationListener
							Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
							ApplicationListener<?> applicationListener =
									factory.createApplicationListener(beanName, targetType, methodToUse);
							if (applicationListener instanceof ApplicationListenerMethodAdapter) {
								((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
							}
                        // 将所生成的  ApplicationListener 实例注册到容器   
							context.addApplicationListener(applicationListener);
							break;
						}
					}
				}
				if (logger.isDebugEnabled()) {
					logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" +
							beanName + "': " + annotatedMethods);
				}
			}
		}
	}
	/**
	 * Determine whether the given class is an {@code org.springframework}
	 * bean class that is not annotated as a user or test {@link Component}...
	 * which indicates that there is no {@link EventListener} to be found there.
	 * @since 5.1
	 */
	private static boolean isSpringContainerClass(Class<?> clazz) {
		return (clazz.getName().startsWith("org.springframework.") &&
				!AnnotatedElementUtils.isAnnotated(ClassUtils.getUserClass(clazz), Component.class));
	}
}

到此这篇关于Spring中的EventListenerMethodProcessor组件详解的文章就介绍到这了,更多相关EventListenerMethodProcessor组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java实现1M图片压缩优化到100kb实现示例

    java实现1M图片压缩优化到100kb实现示例

    这篇文章主要为大家介绍了java实现1M图片压缩优化到100kb示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 将Java程序的输出结果写到txt文件中的方法

    将Java程序的输出结果写到txt文件中的方法

    今天小编就为大家分享一篇将Java程序的输出结果写到txt文件中的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Java实战之邮件的撰写和发送

    Java实战之邮件的撰写和发送

    这篇文章主要为大家详细介绍了通过Java代码实现邮件的撰写和发送功能,文中示例代码介绍的非常详细,具有一定的参考价值,需要的小伙伴们可以学习一下
    2021-11-11
  • java this 用法详解及简单实例

    java this 用法详解及简单实例

    这篇文章主要介绍了java this 用法详解及简单实例的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java集合之Map接口的实现类精解

    Java集合之Map接口的实现类精解

    Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;Map中的键值对以Entry类型的对象实例形式存在;键(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值
    2021-09-09
  • Spring JPA deleteInBatch导致StackOverflow问题

    Spring JPA deleteInBatch导致StackOverflow问题

    这篇文章主要介绍了Spring JPA deleteInBatch导致StackOverflow问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • mybatis主表与明细表一对多的同时插入操作方法

    mybatis主表与明细表一对多的同时插入操作方法

    对主表(采购申请表)和明细表(申请物资表)同时进行插入操作insert,怎么实现呢,下面给大家分享mybatis主表与明细表一对多的同时插入操作方法,感兴趣的朋友一起看看吧
    2023-02-02
  • SpringBoot 项目打成 jar后加载外部配置文件的操作方法

    SpringBoot 项目打成 jar后加载外部配置文件的操作方法

    这篇文章主要介绍了SpringBoot 项目打成 jar后加载外部配置文件的操作方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • SpringBootTest测试时不启动程序的问题

    SpringBootTest测试时不启动程序的问题

    这篇文章主要介绍了SpringBootTest测试时不启动程序的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringBoot2.x的依赖管理配置

    SpringBoot2.x的依赖管理配置

    这篇文章主要介绍了SpringBoot2.x的依赖管理配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06

最新评论