文章目录
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的主要业务逻辑:
- 查找带@EventListener注解的方法 、
- 不为空的话,遍历找到的方法
- 在遍历方法的循环中,遍历EventListenerFactory,如果支持,实例化
ApplicationListenerMethodAdapter
- 初始化完成后, 向application中注册applicationListener
发布事件
基于@EventListener注解的,发布事件流程和基于接口的一样,唯一的区别在于
跟进去走到 listener.onApplicationEvent(event) ,基于注解的会走到 ApplicationListenerMethodAdapter实现类中 onApplicationEvent方法,基于注解的是反射调用,而基于接口的形式是直接调用实现类的onApplicationEvent
onApplicationEvent调用了processEvent
反射调用
基于接口,可以参考我的前面的博客: Spring5源码 - 10 Spring事件监听机制_应用篇
附 <异步派发和异常处理器>
- 如果事件派发器设置了 Executor,则异步多线程的事件派发
- 如果事件派发器设置了 ErrorHandler,则用异常处理器来处理异常