栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

监听器源码详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

监听器源码详解

本文是Spring系列文章的一部分。其他相关文章见

Spring系列文章导航
本文是监听器源码详解。

文章目录
  • 前言
  • 一、监听器的相关类
    • 1.事件
    • 2.监听器
    • 3.事件发布者
  • 二、执行流程
  • 总结


前言 一、监听器的相关类 1.事件


如图所示,Spring的事件机制中,最重要的四个事件必然是和容器生命周期相关的四个类。
在4.2之后,Spring引入了PayloadApplicationEvent 泛型类,可以用泛型类来包装其他类。

2.监听器

SmartApplicationListener
GenericApplicationListener
后者区别在于,supportsEventType方法中使用了ResolvableType。

//SmartApplicationListener
boolean supportsEventType(Class eventType);
//GenericApplicationListener
boolean supportsEventType(ResolvableType eventType);
3.事件发布者

ApplicationEventPublisher

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

从源码可以发现,容器就是一个事件发布者。

ApplicationEventMulticaster

允许多线程执行的时间发布者

	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);
			}
		}
	}
二、执行流程
总结
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号