当前位置: 首页 > news >正文

SpringBoot源码阅读(4)——事件

从监听器到事件

SpringApplication运行中触发事件,多播器发送事件到监听器,监听器处理事件。
SpingApplication中事件都是经过SpringApplicationRunListeners类传送到各个监听器。
以starting事件为例

void starting(ConfigurableBootstrapContext bootstrapContext, Class<?> mainApplicationClass) {doWithListeners("spring.boot.application.starting", (listener) -> listener.starting(bootstrapContext),(step) -> {if (mainApplicationClass != null) {step.tag("mainApplicationClass", mainApplicationClass.getName());}});
}
private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction,Consumer<StartupStep> stepAction) {StartupStep step = this.applicationStartup.start(stepName);this.listeners.forEach(listenerAction);if (stepAction != null) {stepAction.accept(step);}step.end();
}

这里的this.listeners是一个SpringApplicationRunListener的集合,而SpringApplicationRunListener的实现类配置中只有一个EventPublishingRunListener
starting事件执行的是EventPublishingRunListenerstarting方法

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

这里就找到了第一个事件ApplicationStartingEvent
接着往下执行

public void multicastEvent(ApplicationEvent event) {multicastEvent(event, resolveDefaultEventType(event));
}@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类这里略过,里面涉及的内容较多,在这里的具体作用是从类的注解、泛型、继承结构、代理上获取原始的目标对象。
这里要注意的方法getApplicationListeners,根据事件类型获取监听器,然后执行。
查找的过程比较长,这里列举一些比较重要的方法
AbstractApplicationEventMulticastergetApplicationListenersretrieveApplicationListenerssupportsEvent
supportsEvent方法中有具体的判断逻辑

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));
}

根据监听器的类型调用监听器类的支持事件类型supportsEventType和支持事件源类型supportsSourceType两个方法,来判断是否传播到该监听器。
ApplicationListener监听器有多个实现。

  • ClearCachesApplicationListener
  • ParentContextCloserApplicationListener
  • FileEncodingApplicationListener
  • AnsiOutputApplicationListener
  • DelegatingApplicationListener
  • LoggingApplicationListener
  • EnvironmentPostProcessorApplicationListener
  • BackgroundPreinitializer

ClearCachesApplicationListener

结构:
implements ApplicationListener<ContextRefreshedEvent>
支持的事件类型:

  • ContextRefreshedEvent
  • ApplicationContextEvent
  • ApplicationEvent

支持的事件源:
无限制

ParentContextCloserApplicationListener

结构:
implements ApplicationListener<ParentContextAvailableEvent>, ApplicationContextAware, Ordered
支持的事件类型

  • ParentContextAvailableEvent
  • ApplicationEvent

支持的事件源:
无限制

FileEncodingApplicationListener

结构:
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered
支持的事件类型:

  • ApplicationEnvironmentPreparedEvent
  • SpringApplicationEvent
  • ApplicationEvent

支持的事件源:
无限制

AnsiOutputApplicationListener

结构:
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered
支持的事件类型:

  • ApplicationEnvironmentPreparedEvent
  • SpringApplicationEvent
  • ApplicationEvent

支持的事件源:
无限制

DelegatingApplicationListener

结构:
implements ApplicationListener<ApplicationEvent>, Ordered
支持的事件类型:

  • ApplicationEvent

支持的事件源:
无限制

LoggingApplicationListener

结构:
implements GenericApplicationListener
支持的事件类型:

  • ApplicationStartingEvent
  • ApplicationEnvironmentPreparedEvent
  • ApplicationPreparedEvent
  • ContextClosedEvent
  • ApplicationFailedEvent

支持的事件源:

  • SpringApplication
  • ApplicationContext

EnvironmentPostProcessorApplicationListener

结构:
implements SmartApplicationListener, Ordered
支持的事件类型:

  • ApplicationEnvironmentPreparedEvent
  • ApplicationPreparedEvent
  • ApplicationFailedEvent

支持的事件源:

  • SpringApplication
  • ApplicationContext

BackgroundPreinitializer

结构:
implements ApplicationListener<SpringApplicationEvent>
支持的事件类型:

  • SpringApplicationEvent
  • ApplicationEvent

支持的事件源:
无限制

事件

EventPublishingRunListener的方法还有上面的事件类型,SpringBoot中的事件类型有:

  • ApplicationStartingEvent
  • ApplicationEnvironmentPreparedEvent
  • ApplicationContextInitializedEvent
  • ApplicationPreparedEvent
  • ApplicationStartedEvent
  • ApplicationReadyEvent

除了multicastEvent多播事件,还有下面两个方法发布事件。

@Override
public void started(ConfigurableApplicationContext context, Duration timeTaken) {context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context, timeTaken));AvailabilityChangeEvent.publish(context, LivenessState.CORRECT);
}@Override
public void ready(ConfigurableApplicationContext context, Duration timeTaken) {context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context, timeTaken));AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC);
}

事实上,这些事件都在一个包路径下。
在这里插入图片描述

ApplicationStartingEvent

注释:

事件在启动SpringApplication后尽早发布——在Environment或ApplicationContext可用之前,但在ApplicationListeners注册之后。事件的来源是SpringApplication本身,但要注意在早期阶段不要过多地使用其内部状态,因为它可能会在生命周期的后期被修改。

使用的翻译,不甚明了,得看看怎么这个事件触发后,监听器做了什么。

ApplicationEnvironmentPreparedEvent

注释:

当SpringApplication启动并且环境首次可用于检查和修改时发布的事件

ApplicationContextInitializedEvent

注释:

在启动SpringApplication、准备ApplicationContext和调用ApplicationContextInitializer时发布的事件,但在加载任何bean定义之前。

ApplicationPreparedEvent

注释:

当SpringApplication正在启动并且ApplicationContext已完全准备好但未刷新时发布的事件。将加载bean定义,并且环境已准备好在此阶段使用

ApplicationStartedEvent

注释:

刷新应用程序上下文后,但在调用任何应用程序和命令行运行程序之前发布的事件

ApplicationReadyEvent

注释:

事件尽可能晚地发布,以指示应用程序已准备好为请求提供服务。事件的来源是SpringApplication本身,但要注意修改其内部状态,因为届时所有初始化步骤都已完成

ApplicationFailedEvent

注释:

SpringApplication在启动失败时发布的事件

相关文章:

SpringBoot源码阅读(4)——事件

从监听器到事件 SpringApplication运行中触发事件&#xff0c;多播器发送事件到监听器&#xff0c;监听器处理事件。 SpingApplication中事件都是经过SpringApplicationRunListeners类传送到各个监听器。 以starting事件为例 void starting(ConfigurableBootstrapContext boo…...

EDI安全:如何在2024年保护您的数据免受安全和隐私威胁

电子数据交换&#xff08;EDI&#xff09;支持使用标准化格式在组织之间自动交换业务文档。这种数字化转型彻底改变了业务通信&#xff0c;消除了对纸质交易的需求并加速了交易。然而&#xff0c;随着越来越依赖 EDI 来传输发票、采购订单和发货通知等敏感数据&#xff0c;EDI …...

RabbitMQ快速入门 - 图像化界面的简单操作

目录 1、RabbitMQ的安装 2、RabbitMQ基本介绍 3、简单案例 4、数据隔离 1、RabbitMQ的安装 官网链接&#xff1a;rabbitmq官网 &#xff08;官网很详细&#xff0c;也可以在官网学习啦~&#xff09; 基础入门&#xff1a;自主学习&#xff1a;最新版本&#xff1a;安装我…...

新版亚组交互效应函数(P for interaction)newscitb5 1.3版本发布--用于一键生成交互效应表

在SCI文章中&#xff0c;交互效应表格&#xff08;通常是表五&#xff09;能为文章锦上添花&#xff0c;增加文章的信服力&#xff0c;增加结果的可信程度&#xff0c;还能进行数据挖掘。什么是亚组&#xff0c;通常就是特殊类型人群&#xff0c;比如男女&#xff0c;种族等&am…...

gpt讲 Observable 对象

什么是 Observable&#xff1f; Observable 是一种用于处理异步数据流的数据类型。它可以发出多个值&#xff0c;这些值可以是同步或者异步产生的&#xff0c;并且可以在时间上发生变化。在 Angular 中&#xff0c;HttpClient 返回的响应对象、事件流以及许多其他异步任务都可…...

STM32的SPI接口详解

目录 1.SPI简介 2.SPI工作原理 3.SPI时序 3.1 CPOL&#xff08;Clock Polarity&#xff0c;时钟极性&#xff09;&#xff1a; 3.2 CPHA&#xff08;Clock Phase&#xff0c;时钟相位&#xff09;&#xff1a; 3.3 四种工作模式 4.相关代码 4.1使能片选信号 4.2使能通…...

谷歌+火狐浏览器——实现生成二维码并实现拖动——js技能提升

最新遇到的问题&#xff1a;前两个二维码拖动不了&#xff0c;只有第三个一维码生成后&#xff0c;才可以拖拽 【问题】&#xff1a;出现在都是绝对定位&#xff0c;但是没有指定z-index导致的。 解决办法&#xff1a;在方法中添加一个变量 renderDrag(id) {var isDragging f…...

128陷阱详解(从源码分析)

1、128陷阱描述 Integer 整型 -128~127 超过这个范围&#xff0c;比较会不准确 例子 public static void main(String[] args) {Integer a128;Integer b128;Integer e127;Integer f127;System.out.println(ab); //输出falseSystem.out.println(a.equals(b)); //输出trueS…...

点云处理实操 基于多项式重构的平滑和法线估计

目录 基于多项式重构的平滑和法线估计 一、什么是移动最小二乘法(Moving Least Squares, MLS) 二、算法原理 三、算法应用 四、PCL代码实践 基于多项式重构的平滑和法线估计 原文地址:Smoothing and normal estimation based on polynomial reconstruction — Point Cl…...

MongoDB在Linux下的安装与基本操作指南

MongoDB是一款高性能、开源的NoSQL数据库系统&#xff0c;特别适合处理大规模数据存储应用。本文将详细介绍MongoDB在Linux系统&#xff08;以Ubuntu为例&#xff09;上的安装步骤&#xff0c;以及如何进行基本的数据库操作&#xff0c;帮助你快速上手MongoDB。 一、在Linux上…...

端口占用快速解决

netstat -aon|findsstr 8989 taskkill /pid 13221 -t -f 命令解释 1.检查网络连接状态 2.终止特定进程 netstat -aon | findstr 8989 netstat: 这是一个网络状态统计工具&#xff0c;可以显示活动的网络连接、路由表、接口表、多播成员信息等。-a: 显示所有连接和监听端口。通…...

普中51单片机:矩阵按键扫描与应用详解(五)

文章目录 引言电路图开发板IO连接矩阵键盘的工作原理行列扫描逐行/逐列扫描 LCD1602代码库代码演示——暴力扫描代码演示——数码管(行列式)代码演示——线翻转法代码演示——LCD1602密码锁 引言 矩阵按键是一种通过行列交叉连接的按键阵列&#xff0c;可以有效地减少单片机I/…...

SQLite Glob 子句

SQLite Glob 子句 SQLite 的 GLOB 子句是一种强大的搜索工具&#xff0c;用于在数据库中执行模式匹配操作。它类似于 SQL 中的 LIKE 子句&#xff0c;但 GLOB 使用的是基于文件系统的通配符&#xff0c;而不是 SQL 的百分比 (%) 和下划线 (_) 通配符。在本文中&#xff0c;我们…...

Redis基础教程(十七):Redis数据备份与恢复

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; &#x1f49d;&#x1f49…...

基于FPGA的千兆以太网设计(1)----大白话解释什么是以太网

1、什么是以太网? 还记得初学以太网的时候,我就被一大堆专业名词给整懵了:什么以太网,互联网,MAC,IP,局域网,万维网,网络分层模型等等等等。慢着!我学的不是以太网吗?怎么出来这么一大堆东西? 啊!以太网究竟是什么?别急,我接下来就尽量用通俗的大白话来给你解释…...

wordpress的restfull API使用教程,之如何用postman调试API,以便能使用vue等前端框架开发主题

文章目录 API开发手册在postman中调试这里以 post 一篇文章为例&#xff0c;讲解如何调试&#xff1a; 步骤 1&#xff1a;生成应用密码步骤 2&#xff1a;配置Postman步骤 3&#xff1a;创建文章 参考链接 API开发手册 官方API手册&#xff1a;https://developer.wordpress.o…...

刷题刷题刷题

P1123 取数游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 思路&#xff1a; 从1,1开始dfs&#xff0c;若行数x>n则立马刷新最大值退出搜索&#xff0c;若y>m则进入下一行从第一列开始搜索即x1,y1&#xff0c;对当前的搜索点x,y的八个方向进行1,因为不能相邻 AC:…...

【Spring Boot】统一异常处理

目录 统一异常处理一. 概念二. 全局异常处理三. 处理特定异常 统一异常处理 一. 概念 其实统一异常是运用了AOP&#xff08;对某一类事情的集中处理&#xff09;的思维&#xff0c;简单概括就是在我们进行前后端数据交互的时候&#xff0c;抛出的任何的异常都能够自动捕获然后…...

java —— tomcat 部署项目

一、通过 war 包部署 1、将项目导出为 war 包&#xff1b; 2、将 war 包放置在 tomcat 目录下的 webapps 文件夹下&#xff0c;该 war 包稍时便自动解析为项目文件夹&#xff1b; 3、启动 tomcat 的 /bin 目录下的 startup.bat 文件&#xff0c;此时即可从浏览器访问项目首页…...

数据库的优点和缺点分别是什么

数据库作为数据存储和管理的核心组件&#xff0c;具有一系列显著的优点&#xff0c;同时也存在一些潜在的缺点。以下是对数据库优点和缺点的详细分析&#xff1a; 数据库的优点 数据一致性&#xff1a;数据库通过事务管理、锁机制、并发控制等技术手段&#xff0c;确保多个用户…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

51c自动驾驶~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制&#xff08;CCA-Attention&#xff09;&#xff0c;…...

汽车生产虚拟实训中的技能提升与生产优化​

在制造业蓬勃发展的大背景下&#xff0c;虚拟教学实训宛如一颗璀璨的新星&#xff0c;正发挥着不可或缺且日益凸显的关键作用&#xff0c;源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例&#xff0c;汽车生产线上各类…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

Android15默认授权浮窗权限

我们经常有那种需求&#xff0c;客户需要定制的apk集成在ROM中&#xff0c;并且默认授予其【显示在其他应用的上层】权限&#xff0c;也就是我们常说的浮窗权限&#xff0c;那么我们就可以通过以下方法在wms、ams等系统服务的systemReady()方法中调用即可实现预置应用默认授权浮…...

Linux 中如何提取压缩文件 ?

Linux 是一种流行的开源操作系统&#xff0c;它提供了许多工具来管理、压缩和解压缩文件。压缩文件有助于节省存储空间&#xff0c;使数据传输更快。本指南将向您展示如何在 Linux 中提取不同类型的压缩文件。 1. Unpacking ZIP Files ZIP 文件是非常常见的&#xff0c;要在 …...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

tauri项目,如何在rust端读取电脑环境变量

如果想在前端通过调用来获取环境变量的值&#xff0c;可以通过标准的依赖&#xff1a; std::env::var(name).ok() 想在前端通过调用来获取&#xff0c;可以写一个command函数&#xff1a; #[tauri::command] pub fn get_env_var(name: String) -> Result<String, Stri…...