SpringBoot源码阅读(3)——监听器

news2024/9/24 9:28:31

ApplicationListener类初始化位置

在类SpringApplication的构造方法,第267行
在这里插入图片描述
META-INFO/spring.factories中配置的实现类
spring-boot

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener

spring-boot-autoconfigure

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

相关类

在这里插入图片描述

  • ApplicationEvent 事件祖先类
  • org.springframework.context.event.ApplicationEventMulticaster 事件多播器
  • org.springframework.context.event.SmartApplicationListener 监听器
  • org.springframework.context.event.GenericApplicationListener 监听器
  • org.springframework.context.event.EventListener 监听器

SpringBoot启动过程中的事件,都由org.springframework.context.event.ApplicationEventMulticaster播出去,让监听器处理。

监听器触发逻辑

从加载开始

入口在SpringApplication的构造方法,第267行
类工厂加载ApplicationListener的实现类

setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

然后在SpringApplication的实例方法run中,第297行,加载SpringApplicationRunListeners类,这个类中持有多播器的引用。

SpringApplicationRunListeners listeners = getRunListeners(args);

SpringApplicationRunListeners实例化的时候,会通过工厂类加载SpringApplicationRunListener

private SpringApplicationRunListeners getRunListeners(String[] args) {
	Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
	return new SpringApplicationRunListeners(logger,
			getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args),
			this.applicationStartup);
}

SpringApplicationRunListener的实现类

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

EventPublishingRunListener中持有的监听器,就是SpringApplication中加载的ApplicationListener的实现类

public EventPublishingRunListener(SpringApplication application, String[] args) {
	this.application = application;
	this.args = args;
	this.initialMulticaster = new SimpleApplicationEventMulticaster();
	for (ApplicationListener<?> listener : application.getListeners()) {
		this.initialMulticaster.addApplicationListener(listener);
	}
}
  • ClearCachesApplicationListener
  • ParentContextCloserApplicationListener
  • FileEncodingApplicationListener
  • AnsiOutputApplicationListener
  • DelegatingApplicationListener
  • LoggingApplicationListener
  • EnvironmentPostProcessorApplicationListener
  • BackgroundPreinitializer

存储

然后这些监听器被放入了SimpleApplicationEventMulticaster简单多播器里面

@Override
public void addApplicationListener(ApplicationListener<?> listener) {
	synchronized (this.defaultRetriever) {
		// Explicitly remove target for a proxy, if registered already,
		// in order to avoid double invocations of the same listener.
		Object singletonTarget = AopProxyUtils.getSingletonTarget(listener);
		if (singletonTarget instanceof ApplicationListener) {
			this.defaultRetriever.applicationListeners.remove(singletonTarget);
		}
		this.defaultRetriever.applicationListeners.add(listener);
		this.retrieverCache.clear();
	}
}

默认的defaultRetriever则是一个内部类DefaultListenerRetriever
在这里插入图片描述

触发

会在多个地方触发,比如
SpringApplicationrun方法,第298行

listeners.starting(bootstrapContext, this.mainApplicationClass);

触发过程又涉及到事件的设计

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1906215.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一分钟教会你,iPhone和安卓手机虚拟摄像头如何连接电脑?手机连接电脑虚拟摄像头!

在当今的远程工作和在线学习时代&#xff0c;摄像头的需求日益增长。然而&#xff0c;有时我们可能会遇到电脑摄像头画质不佳或无法使用的情况。此时&#xff0c;利用手机的高清摄像头作为电脑的虚拟摄像头&#xff0c;便成了一个理想的选择。本文将为您介绍如何将iPhone和安卓…

SpringMVC系列十三: SpringMVC执行流程 - 源码分析

源码分析 执行流程图实验设计前端控制器分发请求处理器映射器处理器适配器调用目标方法调用视图解析器渲染视图作业布置 执行流程图 实验设计 1.新建com.zzw.web.debug.HelloHandler Controller public class HelloHandler {//编写方法, 响应请求, 返回ModelAndViewRequestMa…

拥抱 AGI:PieDataCS 引领云原生数据计算系统新范式

自2023年后&#xff0c;人工智能技术进入了一个更为成熟和广泛应用的阶段&#xff0c;人工通用智能&#xff08;AGI&#xff09;这一概念也成为了科技界和产业界热议的焦点。本文将结合 AGI 时代背景&#xff0c;从架构设计到落地实践&#xff0c;详细介绍拓数派云原生数据计算…

掌握全球通用的建模语言,从《Archimate®3.2规范参考卡》开始!

作为企业架构设计的通用语言&#xff0c;Archimate更是一种愿景的体现&#xff0c;一个变革的引擎&#xff0c;它不仅可以与TOGAF标准高效结合&#xff0c;还可助力全球企业无边界协作使用&#xff08;下滑可了解更多Archimate建模语言的发展和演进&#xff09;&#xff01; 本…

PMP 认证权威吗?对项目…业生涯的发展有帮助?

PMP认证到底权威吗&#xff1f; 首先在我看来&#xff0c;PMP认证是否权威要从各个角度进行综合考虑。入行这么多年个人也有不少的体会&#xff0c;那么我们就从多个角度进行分析一下&#xff0c;PMP认证的权威性与促进方面。 在深入探讨这个话题前&#xff0c;我分享一下近期…

双向链表 -- 详细理解和实现

欢迎光顾我的homepage 前言 双向链表是一种带头双向循环的链表。在双向链表中&#xff0c;首先存在着一个头结点&#xff1b;其次每个节点有指向下一个节点的指针next 和指向上一个节点的指针prev &#xff1b…

《网络安全和信息化》是什么级别的期刊?是正规期刊吗?能评职称吗?

​问题解答 问&#xff1a;《网络安全和信息化》是不是核心期刊&#xff1f; 答&#xff1a;不是&#xff0c;是知网收录的正规学术期刊。 问&#xff1a;《网络安全和信息化》级别&#xff1f; 答&#xff1a;国家级。主管单位&#xff1a;工业和信息化部 主办单位&#…

脑干出血:揭秘其成因、症状与治疗方法

脑干出血&#xff0c;作为脑出血中尤为凶险的一种&#xff0c;主要袭击脑部的核心——脑干区域。脑干&#xff0c;作为连接大脑与脊髓的桥梁&#xff0c;掌管着呼吸、心跳、意识等至关重要的生命功能&#xff0c;一旦受损&#xff0c;后果不堪设想。 脑干出血的成因 首要元凶…

python爬虫入门(四)之Beautiful Soup库

一、什么是Beautiful Soup库 1、Beautiful Soup库是用来做HTML解析的库 Beautiful Soup把看起来复杂的HTML内容&#xff0c;解析成树状结构&#xff0c;让搜索和修改HTML结构变得更容易 2、第三方库&#xff0c;先安装 终端输入pip install bs4 from bs4 import Beautiful…

AI工具杂谈

AI是在帮助开发者还是取代他们&#xff1f; 在软件开发领域&#xff0c;生成式人工智能&#xff08;AIGC&#xff09;正在改变开发者的工作方式。无论是代码生成、错误检测还是自动化测试&#xff0c;AI工具正在成为开发者的得力助手。然而&#xff0c;这也引发了对开发者职业…

Pytorch的ImageFolder数据加载器

&#x1f4da;博客主页&#xff1a;knighthood2001 ✨公众号&#xff1a;认知up吧 &#xff08;目前正在带领大家一起提升认知&#xff0c;感兴趣可以来围观一下&#xff09; &#x1f383;知识星球&#xff1a;【认知up吧|成长|副业】介绍 ❤️如遇文章付费&#xff0c;可先看…

burpsuite官方靶场之逻辑漏洞篇

*1.过于信任客户端控件* *1.1 达成目标* 用wiener买一件Lightweight l33t leather jacket。 提供的账户&#xff1a;wiener/peter *1.2攻击步骤* 第一步&#xff0c;登录wienr&#xff0c;并且来到主界面&#xff0c;发现Lightweight l33t leather jacket要$1337&#xff…

面试经典 106. 从中序与后序遍历序列构造二叉树

最近小胖开始找工作了&#xff0c;又来刷苦逼的算法了 555 废话不多说&#xff0c;看这一题&#xff0c;上链接&#xff1a;https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/?envTypestudy-plan-v2&envIdtop-inte…

【Unity】简单举例UI合批,优化draw call

1. UGUI-Editor 首先引入UGUI-Editor插件 链接: https://pan.baidu.com/s/1PpxStvgRCDi9xjUr6j6nCQ?pwdm5ju 提取码: m5ju 或者直接去Github搜索UGUI-Editor 2. 没有UI时 3. 放几个UI看看效果 4. 选中Canvas&#xff0c;右键优化Batch 发现减少了3个&#xff0c;这是因为&…

STM32智能无人机控制系统教程

目录 引言环境准备智能无人机控制系统基础代码实现&#xff1a;实现智能无人机控制系统 4.1 数据采集模块 4.2 数据处理与飞行控制 4.3 通信与导航系统实现 4.4 用户界面与数据可视化应用场景&#xff1a;无人机应用与优化问题解决方案与优化收尾与总结 1. 引言 智能无人机控…

vue3创建项目

1. 安装node.js&#xff0c;添加环境变量&#xff0c;确保cmd里能使用node命令以及npm命令&#xff1a;node --version npm --version 本人安装的版本如下&#xff1a; 2. 安装vue的脚手架 npm install -g vue/cli 3. 创建vue项目&#xff1a;1&#xff09;使用ui&#xff1…

#数据结构 链表

单向链表 1. 概念 单向链表 单向循环链表 双向链表 双向循环链表 解决&#xff1a;长度固定的问题&#xff0c;插入和删除麻烦的问题 1、逻辑结构&#xff1a; 线性结构 2、存储结构&#xff1a; 链式存储 链表就是将 结点 用链串起来的线性表&#xff0c;链就是 结点 中的…

开源网页终端webssh容器镜像制作与使用

1.Dockerfile编写&#xff1a; # 指定镜像目标平台与镜像名 alpine表示基础镜像 第一层镜像 FROM --platform$TARGETPLATFORM alpine # 添加元数据到镜像 LABEL maintainer"Jrohy <euvkzxgmail.com>" # 编译时变量 ARG TARGETARCH # 执行编译命令&#xff0c;…

c#第五次作业

目录 1. 实现通用打印泛型类&#xff0c;可以打印各个集合中的值&#xff0c;方便调试 2. 计算遍历目录的耗时 3. 有哪些算术运算符&#xff0c;有哪些关系运算符&#xff0c;有哪些逻辑运算符&#xff0c;有哪些位运算符&#xff0c;有哪些赋值运算符 1&#xff09;算术运算…

我国静止无功发生器(SVG)市场规模逐渐扩大 高压SVG为主流产品

我国静止无功发生器&#xff08;SVG&#xff09;市场规模逐渐扩大 高压SVG为主流产品 静止无功发生器&#xff08;SVG&#xff09;又称为静止同步补偿器、先进静止补偿器、静止调相机等&#xff0c;是利用全控型功率器件组成的桥式变流器来实现动态无功调节的一种先进无功自动补…