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 18 Java/Android
springboot集成flyway自动创表的详细配置
Jun 26 Java/Android
Java中常用解析工具jackson及fastjson的使用
Jun 28 Java/Android
jackson json序列化实现首字母大写,第二个字母需小写
Jun 29 Java/Android
Java实现聊天机器人完善版
Jul 04 Java/Android
java设计模式--原型模式详解
Jul 21 Java/Android
IDEA2021.2配置docker如何将springboot项目打成镜像一键发布部署
Sep 25 Java/Android
springboot中的pom文件 project报错问题
Jan 18 Java/Android
Spring Boot项目传参校验的最佳实践指南
Apr 05 Java/Android
Elasticsearch 配置详解
Apr 19 Java/Android
SpringBoot深入分析讲解监听器模式下
Jul 15 Java/Android
springboot+rabbitmq实现智能家居实例详解
Jul 23 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
MySQL GBK→UTF-8编码转换
2007/05/24 PHP
PHP 采集心得技巧
2009/05/15 PHP
php5.3中连接sqlserver2000的两种方法(com与ODBC)
2012/12/29 PHP
php中使用$_REQUEST需要注意的一个问题
2013/05/02 PHP
php下载excel无法打开的解决方法
2013/12/24 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
php微信开发之关键词回复功能
2018/06/13 PHP
PHP实现的pdo连接数据库并插入数据功能简单示例
2019/03/30 PHP
js中有关IE版本检测
2012/01/04 Javascript
javascript:history.go()和History.back()的区别及应用
2012/11/25 Javascript
JS图片预加载 JS实现图片预加载应用
2012/12/03 Javascript
Javascript之this关键字深入解析
2013/11/12 Javascript
基于Jquery插件Uploadify实现实时显示进度条上传图片
2020/03/26 Javascript
nodejs基础知识
2017/02/03 NodeJs
Bootstrap标签页(Tab)插件使用方法
2017/03/21 Javascript
微信小程序选择图片和放大预览图片功能
2017/11/02 Javascript
vue.js中引入vuex储存接口数据及调用的详细流程
2017/12/14 Javascript
详解webpack-dev-server 设置反向代理解决跨域问题
2018/04/18 Javascript
JS数组求和的常用方法实例小结
2019/01/07 Javascript
使用node搭建自动发图文微博机器人的方法
2019/03/22 Javascript
使用 Vue 实现一个虚拟列表的方法
2019/08/20 Javascript
javascript实现蒙版与禁止页面滚动
2020/01/11 Javascript
vue如何在用户要关闭当前网页时弹出提示的实现
2020/05/31 Javascript
Python中使用Beautiful Soup库的超详细教程
2015/04/30 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
Python中使用gflags实例及原理解析
2019/12/13 Python
python如何使用腾讯云发送短信
2020/09/17 Python
纯css3制作网站后台管理面板
2014/12/30 HTML / CSS
Expedia印度:您的一站式在线旅游网站
2017/08/24 全球购物
馥绿德雅美国官方网站:Rene Furterer头皮护理专家
2019/05/01 全球购物
日本动漫周边服饰销售网站:Atsuko
2019/12/16 全球购物
W Hamond官网:始于1979年的钻石专家
2020/07/20 全球购物
电大毕业生自我鉴定
2014/04/10 职场文书
骨干教师事迹材料
2014/12/17 职场文书
文明班级申报材料
2014/12/24 职场文书
Python实现byte转integer
2021/06/03 Python