Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析


在这里插入图片描述


Pre

Spring5源码 - 10 Spring事件监听机制_应用篇

Spring5源码 - 11 Spring事件监听机制_源码篇

Spring5源码 - 12 Spring事件监听机制_异步事件监听应用及源码解析

通过上面三篇文章的学习,是不是发现没有看到Spring是如何解析@EventListener注解的呢? Let’s go ~~~


概览

Spring容器在启动的时候初始化EventListenerMethodProcessor和DefaultEventListenerFactory,用于处理@EventListener注解, 调用EventListenerMethodProcessor的afterSingletonsInstantiated方法。

在这里插入图片描述


开天辟地的时候初始化的处理器

	/**
		 * 处理监听方法的注解解析器EventListenerMethodProcessor
		 */
		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

		/**
		 * 注册事件监听器工厂
		 */
		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

一个是注册一个用于解析@EventListener注解的EventListenerMethodProcessor ,一个是注册事件监听工厂。


@EventListener

在这里插入图片描述


EventListenerMethodProcessor

我们来看下EventListenerMethodProcessor的类继承结构

在这里插入图片描述

EventListenerMethodProcessor 实现了 EventListenerMethodProcessor,所以执行 BeanFactory 后置处理器时,会调用 postProcessBeanFactory(),将 DefaultEventListenerFactory 添加到缓存中。

可以看到EventListenerMethodProcessor实现了SmartInitializingSingleton接口 , 那肯定要重写 afterSingletonsInstantiated方法。

我们知道SmartInitializingSingleton接口是在所有的Bean实例化完成以后,Spring回调的方法。 获取所有的 BeanFactory,找到其中标注了 @EventListener 的方法,利用反射和 DefaultEventListenerFactory 为其创建 ApplicationListener,并添加到事件派发器的缓存中。

refresh----> finishBeanFactoryInitialization(beanFactory);---->preInstantiateSingletons()

在这里插入图片描述


afterSingletonsInstantiated

我们看下 EventListenerMethodProcessor的afterSingletonsInstantiated方法

	@Override
	public void afterSingletonsInstantiated() {
	         //从BeanFactory中获取EventListenerFactory,EventListenerFactory共有2个实现,一个是DefaultEventListenerFactory,对普通的@EventListener进行解析,另一个是TransactionalEventListenerFactory,可以对@TransactionalEventListener进行解析。

		List<EventListenerFactory> factories = getEventListenerFactories();
		
		ConfigurableApplicationContext context = getApplicationContext();
		// 获取所有的beanNames
		String[] beanNames = context.getBeanNamesForType(Object.class);
		// 遍历循环
		for (String beanName : beanNames) {
			  ........
			           // 关键:处理bean 
						processBean(factories, beanName, type);
			  .........
			}
		}
	}
protected void processBean(final List<EventListenerFactory> factories, final String beanName, final Class<?> targetType) {
        if (!this.nonAnnotatedClasses.contains(targetType)) {
            Map<Method, EventListener> annotatedMethods = null;
            try {
                //查找带@EventListener注解的方法
                annotatedMethods = MethodIntrospector.selectMethods(targetType,
                        new MethodIntrospector.MetadataLookup<EventListener>() {
                            @Override
                            public EventListener inspect(Method method) {
                                return AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class);
                            }
                        });
            }
            catch (Throwable ex) {
          				.......
            }
            //如果没有找到带EventListener注解的方法
            if (CollectionUtils.isEmpty(annotatedMethods)) {
                this.nonAnnotatedClasses.add(targetType);
                ........
            }
            //否则
            else {
                // Non-empty set of methods
                //遍历
                for (Method method : annotatedMethods.keySet()) {
                    for (EventListenerFactory factory : factories) {
                        //EventListenerFactory是否支持对该method的处理
                        if (factory.supportsMethod(method)) {
                            Method methodToUse = AopUtils.selectInvocableMethod(
                                    method, this.applicationContext.getType(beanName));
                            // 创建事件监听器
                            ApplicationListener<?> applicationListener =
                                    factory.createApplicationListener(beanName, targetType, methodToUse); 
                            if (applicationListener instanceof ApplicationListenerMethodAdapter) {
                                ((ApplicationListenerMethodAdapter) applicationListener)
                                        .init(this.applicationContext, this.evaluator);
                            }
                            // 注册事件到Context中
                            this.applicationContext.addApplicationListener(applicationListener);
                            break;
                        }
                    }
                }
                .......
            }
        }
    }

在这里插入图片描述


小结

processBean的主要业务逻辑:

  1. 查找带@EventListener注解的方法 、
  2. 不为空的话,遍历找到的方法
  3. 在遍历方法的循环中,遍历EventListenerFactory,如果支持,实例化ApplicationListenerMethodAdapter
  4. 初始化完成后, 向application中注册applicationListener

发布事件

在这里插入图片描述

基于@EventListener注解的,发布事件流程和基于接口的一样,唯一的区别在于

跟进去走到 listener.onApplicationEvent(event) ,基于注解的会走到 ApplicationListenerMethodAdapter实现类中 onApplicationEvent方法,基于注解的是反射调用,而基于接口的形式是直接调用实现类的onApplicationEvent

在这里插入图片描述

onApplicationEvent调用了processEvent

在这里插入图片描述

反射调用

在这里插入图片描述

基于接口,可以参考我的前面的博客: Spring5源码 - 10 Spring事件监听机制_应用篇


附 <异步派发和异常处理器>

  • 如果事件派发器设置了 Executor,则异步多线程的事件派发
  • 如果事件派发器设置了 ErrorHandler,则用异常处理器来处理异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值