【spring源码学习】spring的事件发布监听机制源码解析
【一】相关源代码类
(1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticaster.
=>该类的初始化是放在项目加载时,在ioc容器xml配置文件解析加载完毕后,注册bean创建前后置处理实现类(BeanPostProcessor 接口实现),beanFactory配置处理(BeanFactoryPostProcessor接口实现)后,初始化该事件发布监听机制的核心类。
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // 解析xml配置文件
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try {
// 注册未来bean实例化的前后置处理的PostProcessor接口实现
postProcessBeanFactory(beanFactory); //执行所有实现BeanFactoryPostProcessor接口实现,对beanFactory进行处理
invokeBeanFactoryPostProcessors(beanFactory); // 注册未来bean实例化的前后置处理的PostProcessor接口实现
registerBeanPostProcessors(beanFactory); // 注册未来bean实例化的前后置处理的PostProcessor接口实现
initMessageSource(); // 实例化spring事件发布监听机制的核心类,SimpleApplicationEventMulticaster
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
onRefresh(); // 注册事件监听器
registerListeners(); // 实例化非懒加载的bean,完成ioc容器中bean的实例化和反转依赖,并在内部实现动态代理相关的操作
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex); // Destroy already created singletons to avoid dangling resources.
destroyBeans(); // Reset 'active' flag.
cancelRefresh(ex); // Propagate exception to caller.
throw ex;
}
}
}
(2)spring的事件发布监听机制的监听器类的接口:org.springframework.context.ApplicationListener<E extends ApplicationEvent>
=>监听器的定义必须实现该接口。
=>所监听的事件也必须继承org.springframework.context.ApplicationEvent的抽象类
=>在初始化管理类后,会注册监听器。会从beanFactory里得到所有实现ApplicationListener接口的bean的名字,并注册到ListenerRetriever的属性applicationListenerBeans集合中。
(3)spring的事件发布监听机制的事件的基础类:org.springframework.context.ApplicationEvent
=>事件的事件类必须继承该基类
=>定义监听器的同时,必须定义相关的事件类。
(4)spring的事件发布监听机制的存储事件监听的类:org.springframework.context.event.AbstractApplicationEventMulticaster.ListenerRetriever
=>该类内部存储了一个n个事件对应的多个监听器
=>属性Set<ApplicationListener<?>> applicationListeners 存储的是事件监听器的实例
=>属性Set<String> applicationListenerBeans 存储的事件监听器在ioc容器中的beanNames
(5)spring的事件发布监听机制的存储事件体的类:org.springframework.context.event.AbstractApplicationEventMulticaster.ListenerCacheKey
=>一个事件体的class类型+事件源(事件体内存储的数据)的class类型决定由那些监听监听器处理该事件
=>属性Class<?> eventType 存储的事件体的class类型
=>属性Class<?> sourceType 存储的事件源的class类型(事件体内数据)
【二】设计思想
(1)通过事件体,拿到事件类的cls1类型,和事件源(事件体内部存储的数据类)的cls类型
(2)然后在IOC容器中所有实现ApplicationListener接口的事件监听器中进行匹配。找到所有适合该事件的事件监听器集合。将事件监听集合形成ListenerRetriever对象。
=>从IOC容器中的一个个事件监听器实现类的范型填充类的类型(也就是事件体的 类型)cls2
=>Listener可能是代理对象(因为@Async注解),Listenner的ioc在实例化阶段,会被spring创建成代理对象。spring内部也会做处理,得到代理对象代理的targetClass,也就是Listenner的真实类型。
=>看当前事件体的类型cls1是否是事件监听器的范型填充类的类型cls2的子类或本身。如果是,则表示这个事件监听器匹配该事件。
(3)将该事件体,的事件类的cls1类型+事件源的cls类型。形成ListenerCacheKey对象,以此为key,以事件监听集合ListenerRetriever对象作为value,存储在AbstractApplicationEventMulticaster类中private final Map<ListenerCacheKey, ListenerRetriever> retrieverCache = new ConcurrentHashMap<ListenerCacheKey, ListenerRetriever>(64);属性中作为cache,方便下次匹配,不再从ioc容器中重新匹配。
【三】事件缓冲,扩展。hasMap中以对象作为key。需要重写hasCode方法,equals方法
private static class ListenerCacheKey { private final Class<?> eventType; private final Class<?> sourceType; public ListenerCacheKey(Class<?> eventType, Class<?> sourceType) {
this.eventType = eventType;
this.sourceType = sourceType;
} @Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
ListenerCacheKey otherKey = (ListenerCacheKey) other;
return ObjectUtils.nullSafeEquals(this.eventType, otherKey.eventType) &&
ObjectUtils.nullSafeEquals(this.sourceType, otherKey.sourceType);
} @Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.eventType) * 29 + ObjectUtils.nullSafeHashCode(this.sourceType);
}
}
【spring源码学习】spring的事件发布监听机制源码解析的更多相关文章
- 深入理解Spring的容器内事件发布监听机制
目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...
- Spring事件监听机制源码解析
Spring事件监听器使用 1.Spring事件监听体系包括三个组件:事件.事件监听器,事件广播器. 事件:定义事件类型和事件源,需要继承ApplicationEvent. package com.y ...
- Spring Boot(六)自定义事件及监听
事件及监听并不是SpringBoot的新功能,Spring框架早已提供了完善的事件监听机制,在Spring框架中实现事件监听的流程如下: 自定义事件,继承org.springframework.con ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- Spring笔记(7) - Spring的事件和监听机制
一.背景 事件机制作为一种编程机制,在很多开发语言中都提供了支持,同时许多开源框架的设计中都使用了事件机制,比如SpringFramework. 在 Java 语言中,Java 的事件机制参与者有3种 ...
- 【cocos2d-js官方文档】事件分发监听机制(摘录)
简介 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处理用户交互就需要使用事件监听器了. 总概: 事件监听器(cc.EventListener) 封装用户的事件处理逻 ...
- jsonp和事件发布监听
模拟jsonp var id = 0; function JSONP(url,param,cb){ var callbackName = "json_" + id++; var a ...
- Spring知识点回顾(07)事件发布和监听
Spring知识点回顾(07)事件发布和监听 1.DemoEvent extends ApplicationEvent { public DemoEvent(Object source, String ...
- Spring事件发布与监听机制
我是陈皮,一个在互联网 Coding 的 ITer,微信搜索「陈皮的JavaLib」第一时间阅读最新文章,回复[资料],即可获得我精心整理的技术资料,电子书籍,一线大厂面试资料和优秀简历模板. 目录 ...
随机推荐
- 【笔记】Maven使用入门
参考<maven实战> 1.编写POM 2.编写主代码 3.编写测试代码 4.打包和运行 具体如下: 1.编写POM. <!-- XML头,指定了该xml文档的版本和编辑方式 --& ...
- 使用Angularjs开发Web App 视频课程 --麦子学院课程
前往搓这里: http://www.maiziedu.com/group/common/course/3271/ 查看课程搓这里:http://www.maiziedu.com/course/web/ ...
- 20145219 《Java程序设计》第02周学习总结
20145219 <Java程序设计>第02周学习总结 教材学习内容总结 类型:基本类型.类类型(参考类型) 基本类型: 整数:short占2字节,int占4字节,long占8字节 字节: ...
- Mybatis导入原生配置文件
在Spring-Mybatis中导入Mybatis原生配置文件 在sqlSessionFactory Bean中设置设置configLocation属性 <property name=" ...
- Maven的简介
1 What? 1.1 Maven 简介Maven 是 Apache 软件基金会组织维护的一款自动化构建工具,专注服务于 Java 平台的项目构建和依赖管理.Maven 这个单词的本意是:专家,内行. ...
- quartz(6)--集群
Quartz应用能被集群,是水平集群还是垂直集群取决于你自己的需要.集群提供以下好处: · 伸缩性 · 高可用性 · 负载均衡 目前,Quartz只能借助关系数据库和JDBC作业存储支持集群. qua ...
- mysql入门语句
连接 1.mysql -h localhost -u root -p ******(回车) 2.mysql -h localhost -u root -p(回车) ****** 3.mysql -u ...
- SQLyog常用快捷键 - 转载
SQLyog常用快捷键 1. SQL格式化 F12 格式化当前行所在的SQL Ctrl+F12 格式化选中的SQL 2. 窗口操作 Ctrl+T 打开一个新的查询窗口 Alt+L ...
- 关于Android中根据ID名动态获取资源的两个方法
在开发中, 我们习惯了类似下面这种方式去实现引用资源: context.getResources().getDrawable(R.drawable.flower); 但是,当我们提前知道这个资源的id ...
- 在UIElement外面多套一层布局面板(Grid、StackPanel)的意义
在一个UIElement或多个UIElement外面套上一层布局面板(Grid.StackPanel),可以起到统一管理作用(非重点关注):另外,更重要的是:可以起到扩大UIElement操作有效范围 ...