SpringBoot深入分析讲解监听器模式下


Posted in Java/Android onJuly 15, 2022

我们来以应用启动事件:ApplicationStartingEvent为例来进行说明:

以启动类的SpringApplication.run方法为入口,跟进SpringApplication的两个同名方法后,我们会看到主要的run方法,方法比较长,在这里只贴出与监听器密切相关的关键的部分:

SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();

我们跟进这个starting方法,方法的内容如下:

void starting() {
	for (SpringApplicationRunListener listener : this.listeners) {
		listener.starting();
	}
}

这里的listeners已经在getRunListeners方法中完成了加载,加载原理类似于系统初始化器

starting方法逻辑很简单,就是调用SpringApplicationRunListener的starting方法。下面继续分析这个starting方法:

我们进入了EventPublishingRunListener类(SpringApplicationRunListener 的实现类)的starting方法:

@Override
	public void starting() {
		this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
	}

这里就使用了广播器,来广播新的ApplicationStartingEvent事件。

我们跟进这个multicastEvent方法:

@Override
	public void multicastEvent(ApplicationEvent event) {
		multicastEvent(event, resolveDefaultEventType(event));
	}

继续看同名的方法multicastEvent:

@Override
	public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		Executor executor = getTaskExecutor();
		for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
				invokeListener(listener, event);
			}
		}
	}

这里的ResolvableType 是对event做了包装,我们不去关注;由于我们没有创建线程池,所以executor是空的。我们重点关注两个部分:

1、getApplicationListeners --> 获取所有关注此事件的监听器(※);

2、invokeListener --> 激活监听器;

getApplicationListeners (AbstractApplicationEventMulticaster类中)方法,代码如下:

protected Collection<ApplicationListener<?>> getApplicationListeners(
			ApplicationEvent event, ResolvableType eventType) {
		Object source = event.getSource();
		Class<?> sourceType = (source != null ? source.getClass() : null);
		ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);
		// Quick check for existing entry on ConcurrentHashMap...
		ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
		if (retriever != null) {
			return retriever.getApplicationListeners();
		}
		if (this.beanClassLoader == null ||
				(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
						(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
			// Fully synchronized building and caching of a ListenerRetriever
			synchronized (this.retrievalMutex) {
				retriever = this.retrieverCache.get(cacheKey);
				if (retriever != null) {
					return retriever.getApplicationListeners();
				}
				retriever = new ListenerRetriever(true);
				Collection<ApplicationListener<?>> listeners =
						retrieveApplicationListeners(eventType, sourceType, retriever);
				this.retrieverCache.put(cacheKey, retriever);
				return listeners;
			}
		}
		else {
			// No ListenerRetriever caching -> no synchronization necessary
			return retrieveApplicationListeners(eventType, sourceType, null);
		}
	}

入参中的event就是ApplicationStartingEvent,sourceType是org.springframework.boot.SpringApplication类。ListenerRetriever类型本人将其视作是一个保存监听器的容器。

可以看出,程序首先在缓存里面寻找ListenerRetriever类型的retriever,如果没有找到,加锁再从缓存里面找一次。这里我们缓存里是没有内容的,所以都不会返回。

接下来调用了retrieveApplicationListeners方法,来遍历所有的监听器。retrieveApplicationListeners方法比较长,我们重点关注下supportsEvent(listener, eventType, sourceType)方法,该方法用来判断是否此监听器关注该事件,过程主要包括,判断此类型是否是GenericApplicationListener类型,如果不是,则构造一个代理,代理的目的是,通过泛型解析,最终获得监听器所感兴趣的事件。

如果经过判断,监听器对该事件是感兴趣的,则此监听器会被加入监听器列表中。

protected boolean supportsEvent(
			ApplicationListener<?> listener, ResolvableType eventType, @Nullable Class<?> sourceType) {
		GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
				(GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
		return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
	}

当某个事件所有的监听器被收集完毕后,multicastEvent(SimpleApplicationEventMulticaster类)方法会对事件进行传播。即调用监听器的通用触发接口方法:listener.onApplicationEvent(event);这样,就完成了这个事件的传播。

到此这篇关于SpringBoot深入分析讲解监听器模式下的文章就介绍到这了,更多相关SpringBoot监听器模式内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!


Tags in this post...

Java/Android 相关文章推荐
一篇带你入门Java垃圾回收器
Jun 16 Java/Android
Springboot如何使用logback实现多环境配置?
Jun 16 Java/Android
详解Java ES多节点任务的高效分发与收集实现
Jun 30 Java/Android
SpringBoot读取Resource下文件的4种方法
Jul 02 Java/Android
小程序与后端Java接口交互实现HelloWorld入门
Jul 09 Java/Android
java设计模式--原型模式详解
Jul 21 Java/Android
Java 在生活中的 10 大应用
Nov 02 Java/Android
OpenCV实现反阈值二值化
Nov 17 Java/Android
JVM的类加载器和双亲委派模式你了解吗
Mar 13 Java/Android
SpringBoot2零基础到精通之数据与页面响应
Mar 22 Java/Android
JAVA 线程池(池化技术)的实现原理
Apr 28 Java/Android
Java版 简易五子棋小游戏
May 04 Java/Android
tree shaking对打包体积优化及作用
Jul 07 #Java/Android
MyBatis在注解上使用动态SQL方式(@select使用if)
Jul 07 #Java/Android
一文了解Java动态代理的原理及实现
Jul 07 #Java/Android
Java实现字符串转为驼峰格式的方法详解
Jul 07 #Java/Android
Spring中bean集合注入的方法详解
java中如何截取字符串最后一位
Jul 07 #Java/Android
Java 中的 Lambda List 转 Map 的多种方法详解
Jul 07 #Java/Android
You might like
PHP编码规范之注释和文件结构说明
2010/07/09 PHP
codeigniter发送邮件并打印调试信息的方法
2015/03/21 PHP
详解php用curl调用接口方法,get和post两种方式
2017/01/13 PHP
js静态作用域的功能。
2006/12/25 Javascript
JavaScript将取代AppleScript?
2014/09/18 Javascript
深入理解JavaScript系列(25):设计模式之单例模式详解
2015/03/03 Javascript
js实现登陆遮罩效果的方法
2015/07/28 Javascript
轻松学习Javascript闭包函数
2015/12/15 Javascript
限制只能输入数字的实现代码
2016/05/16 Javascript
jquery获取复选框的值的简单实例
2016/05/26 Javascript
canvas实现绘制吃豆鱼效果
2017/01/12 Javascript
JavaScript数组去重的6个方法
2017/01/21 Javascript
js简易版购物车功能
2017/06/17 Javascript
AjaxUpLoad.js实现文件上传
2018/03/05 Javascript
学习Vue组件实例
2018/04/28 Javascript
vue嵌套路由与404重定向实现方法分析
2018/05/04 Javascript
vuex与组件联合使用的方法
2018/05/10 Javascript
基于vue-cli3和element实现登陆页面
2019/11/13 Javascript
jQuery实现鼠标拖拽登录框移动效果
2020/09/13 jQuery
python将html转成PDF的实现代码(包含中文)
2013/03/04 Python
python格式化字符串实例总结
2014/09/28 Python
python使用cStringIO实现临时内存文件访问的方法
2015/03/26 Python
python通过apply使用元祖和列表调用函数实例
2015/05/26 Python
使用简单工厂模式来进行Python的设计模式编程
2016/03/01 Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
2018/02/11 Python
Python:type、object、class与内置类型实例
2019/12/25 Python
解决Python命令行下退格,删除,方向键乱码(亲测有效)
2020/01/16 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
适合Python初学者的一些编程技巧
2020/02/12 Python
Python连接HDFS实现文件上传下载及Pandas转换文本文件到CSV操作
2020/06/06 Python
详解pycharm配置python解释器的问题
2020/10/15 Python
css3 flex实现div内容水平垂直居中的几种方法
2020/03/27 HTML / CSS
HomeAway澳大利亚:预订你的度假屋,公寓、度假村、别墅等
2019/02/20 全球购物
数据库设计的包括哪两种,请分别进行说明
2016/07/15 面试题
农民工工资承诺书范文
2014/03/31 职场文书
演讲比赛主持词
2015/06/29 职场文书