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

SpringBoot ApplicationEvent详解

ApplicationStartingEvent 阶段
LoggingApplicationListener#onApplicationStartingEvent
初始化日志工厂,LoggingSystemFactory接口,可以通过spring.factories进行定制
可以通过System.setProperty("org.springframework.boot.logging.LoggingSystem","类全路径限定名或者none") 指定log实现类
如果配置的是none,则返回 org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem
springboot默认指定了三种类型:
org.springframework.boot.logging.logback.LogbackLoggingSystem.Factory
org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.Factory
org.springframework.boot.logging.java.JavaLoggingSystem.Factory
----------
BackgroundPreinitializer#onApplicationEvent 在当前阶段不做任何处理!!!
可以通过System.setProperty("spring.backgroundpreinitializer.ignore","true|false")
同时满足服务器是多核cpu并且非GraalVM环境
来指定是否通过后台线程去加载某些资源,默认是单独开一个线程来加载某些资源
线程名称:background-preinit 
后台加载的资源:
    ConversionServiceInitializer.class 
    ValidationInitializer.class 
    MessageConverterInitializer.class 
    JacksonInitializer.class 
    CharsetInitializer.class
有异常直接忽略
----------
DelegatingApplicationListener#onApplicationEvent 在当前阶段不做任何处理!!!
可以通过配置 context.listener.classes 属性来指定要执行的listener,是一个复合包装类
内部定义了SimpleApplicationEventMulticaster事件驱动类,用来指定配置的listener
==========
ApplicationEnvironmentPreparedEvent 阶段
EnvironmentPostProcessorApplicationListener#onApplicationEvent
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
获取ConfigurableEnvironment实例
获取SpringApplication实例
通过SpringApplication实例.getResourceLoader()和ConfigurableEnvironment实例.getBootstrapContext() 获取 EnvironmentPostProcessors
所有实现了 org.springframework.boot.env.EnvironmentPostProcessor 接口的实现类,同样可以通过 spring.factories进行定制
开始遍历
1、RandomValuePropertySourceEnvironmentPostProcessor#postProcessEnvironment
将RandomValuePropertySource添加到systemEnvironment这个属性解析器集合的最后一位
RandomValuePropertySource: 用法,配置属性以random开头
比如:random.int 返回正负32位以内的一个伪随机数字
random.long 返回正负64位以内的一个伪随机数字
random.int(0,9) 包含0不包含9 中的一个伪随机数字
random.long[0,9] 包含0不包含9 中的一个伪随机数字
() 和 [] 或者 $$ 作用都一样 前后任意两个字符括起来,数字使用逗号分隔即可
2、SystemEnvironmentPropertySourceEnvironmentPostProcessor#postProcessEnvironment
判断是否设置了 SpringApplication.environmentPrefix 属性,如果设置了将systemEnvironment(SystemEnvironmentPropertySource)
的数据封装为OriginAwareSystemEnvironmentPropertySource类(SystemEnvironmentPropertySource的子类),有prefix属性.
spring.main.environment-prefix:不允许这样设置
可以通过 SpringApplicationBuilder.environmentPrefix("xx") 来设置,可以以 . - _ 等结尾,
3、SpringApplicationJsonEnvironmentPostProcessor#postProcessEnvironment
解析json属性,使用spring.application.json或者SPRING_APPLICATION_JSON命令行参数指定,按序返回有效的第一个进行解析.
解析成功后,将json属性封装为JsonPropertySource,放到servlet sources前面,如果不是servlet环境,则放在第一位
解析json属性的解析器:
org.springframework.boot.json.JsonParserFactory#getJsonParser: 指定了4种json解析器
com.fasterxml.jackson.databind.ObjectMapper
com.google.gson.Gson
org.yaml.snakeyaml.Yaml
org.springframework.boot.json.BasicJsonParser
按以上顺序进行加载,加载成功就返回对应的对象实例
4、CloudFoundryVcapEnvironmentPostProcessor#postProcessEnvironment 在springcloud环境下生效
判断是否设置了 spring.main.cloud-platform VCAP_APPLICATION VCAP_SERVICES 满足任意一个
设置了 添加 vcap sources
5、ConfigDataEnvironmentPostProcessor#postProcessEnvironment **你个**   加载并且解析设定的配置文件
spring.config.on-not-found: 配置找不到的处理方法,参考ConfigDataNotFoundAction枚举类
SpringApplication.additionalProfiles 通过 builder 构建
加载指定的配置文件并且设置environment中的profiles
# ConfigData Location Resolvers
org.springframework.boot.context.config.ConfigDataLocationResolver=\
org.springframework.boot.context.config.ConfigTreeConfigDataLocationResolver,\
org.springframework.boot.context.config.StandardConfigDataLocationResolver

# ConfigData Loaders
org.springframework.boot.context.config.ConfigDataLoader=\
org.springframework.boot.context.config.ConfigTreeConfigDataLoader,\
org.springframework.boot.context.config.StandardConfigDataLoader
6、DebugAgentEnvironmentPostProcessor#postProcessEnvironment
reactor.tools.agent.ReactorDebugAgent有这个类并且spring.reactor.debug-agent.enabled属性配置为true时
执行 ReactorDebugAgent init() 方法
7、IntegrationPropertiesEnvironmentPostProcessor#postProcessEnvironment
META-INF/spring.integration.properties 这个文件存在时加载内容并且转化为IntegrationPropertiesPropertySource添加到sources结尾
----------
AnsiOutputApplicationListener#onApplicationEvent
进行属性绑定 spring.output.ansi.enabled org.springframework.boot.ansi.AnsiOutput#enabled 参考 Enabled 枚举类
spring.output.ansi.console-available  AnsiOutput.consoleAvailable = consoleAvailable
----------
LoggingApplicationListener#onApplicationEvent
日志文件和属性初始化配置
----------
BackgroundPreinitializer#onApplicationEvent
可以通过System.setProperty("spring.backgroundpreinitializer.ignore","true|false")
同时满足服务器是多核cpu并且非GraalVM环境
来指定是否通过后台线程去加载某些资源,默认是单独开一个线程来加载某些资源
线程名称:background-preinit 
后台加载的资源:
    ConversionServiceInitializer.class 
    ValidationInitializer.class 
    MessageConverterInitializer.class 
    JacksonInitializer.class 
    CharsetInitializer.class
有异常直接忽略
----------
DelegatingApplicationListener#onApplicationEvent
可以通过配置 context.listener.classes 属性来指定要执行的listener,是一个复合包装类
内部定义了SimpleApplicationEventMulticaster事件驱动类,用来指定配置的listener
----------
FileEncodingApplicationListener#onApplicationEvent1
spring.mandatory-file-encoding: 查看是否配置了此属性,强制编码,如果这个与file.encoding不符合报错!!!
==========
bindToSpringApplication(environment) // 将spring.main开头的属性配置绑定到SpringApplication属性上
==========
applyInitializers(context); // 执行ApplicationContextInitializer接口的实现类
==========
ApplicationContextInitializedEvent
BackgroundPreinitializer#onApplicationEvent 此阶段啥也不做!!!
----------
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
==========
ApplicationPreparedEvent
EnvironmentPostProcessorApplicationListener#onApplicationEvent
onApplicationPreparedEvent() > EnvironmentPostProcessorApplicationListener#finish() > DeferredLogs#switchOverAll()
打印日志 
----------
LoggingApplicationListener#onApplicationEvent
注册springBootLoggingSystem单例Bean
logFile存在并且springBootLogFile不存在这个Bean时注册springBootLogFile单例Bean
loggerGroups存在并且springBootLoggerGroups不存在这个Bean时注册springBootLoggerGroups单例Bean
springBootLoggingLifecycle单例Bean不存在BeanFactory.getParent为空时注册springBootLoggingLifecycle单例Bean
----------
BackgroundPreinitializer#onApplicationEvent 此阶段啥也不做!!!
----------
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
==========
中间存在的各种事件驱动类
ServletWebServerInitializedEvent
SpringApplicationAdminMXBeanRegistrar#onApplicationEvent : onWebServerInitializedEvent((WebServerInitializedEvent) event);
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
ServerPortInfoApplicationContextInitializer#onApplicationEvent : 绑定server.ports sources
----------
ContextRefreshedEvent
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
ConditionEvaluationReportLoggingListener.ConditionEvaluationReportListener#onApplicationEvent 打印方法 ConditionEvaluationReportMessage
ClearCachesApplicationListener#onApplicationEvent 清理加载反射field和method的缓存数据,调用类加载器的clearCache方法清理缓存
SharedMetadataReaderFactoryContextInitializer.SharedMetadataReaderFactoryBean#onApplicationEvent 清理加载的class缓存数据
ResourceUrlProvider#onApplicationEvent 静态资源 /webjars/** 和 /static/**
==========
ApplicationStartedEvent
BackgroundPreinitializer#onApplicationEvent 此阶段啥也不做!!!
----------
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
----------
StartupTimeMetricsListener#onApplicationEvent
设置埋点监控 TimeGauge
----------
TomcatMetricsBinder#onApplicationEvent
设置tomcat监控绑定
----------
AvailabilityChangeEvent
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
----------
ApplicationAvailabilityBean#onApplicationEvent 添加到 org.springframework.boot.availability.ApplicationAvailabilityBean#events 集合中 应用启动好了
==========
ApplicationReadyEvent
SpringApplicationAdminMXBeanRegistrar#onApplicationEvent 应用准备好了
----------
BackgroundPreinitializer#onApplicationEvent 此阶段啥也不做!!!
----------
StartupTimeMetricsListener#onApplicationEvent  注册TimeGauge埋点监控
----------
DelegatingApplicationListener#onApplicationEvent 此阶段啥也不做!!!
==========
AvailabilityChangeEvent
ApplicationAvailabilityBean#onApplicationEvent 添加到 org.springframework.boot.availability.ApplicationAvailabilityBean#events 集合中 应用准备好了
 

相关文章:

SpringBoot ApplicationEvent详解

ApplicationStartingEvent 阶段 LoggingApplicationListener#onApplicationStartingEvent 初始化日志工厂,LoggingSystemFactory接口,可以通过spring.factories进行定制 可以通过System.setProperty("org.springframework.boot.logging.LoggingSystem",&q…...

WebSocket 报java.io.IOException: 远程主机强迫关闭了一个现有的连接。

在客户端强制关闭时,或者窗口强制关闭时,后端session没有关闭。 有时还会报:java.io.EOFException: 这个异常 前端心跳没有收到信息,还在心跳。 CloseReason close new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, &…...

关于git约定式提交IDEA

背景 因为git提交的消息不规范导致被乱喷,所以领导统一规定了约定式提交 官话 约定式提交官网地址 约定式提交规范是一种基于提交信息的轻量级约定。 它提供了一组简单规则来创建清晰的提交历史; 这更有利于编写自动化工具。 通过在提交信息中描述功能…...

【计算机网络】http协议

目录 前言 认识URL URLEncode和URLDecode http协议格式 http方法 GET POST GET与POST的区别 http状态码 http常见header 简易的http服务器 前言 我们在序列化和反序列化这一章中,实现了一个网络版的计算器。这个里面设计到了对协议的分析与处…...

仓库太大,clone 后,git pull 老分支成功,最新分支失败

由于 git 仓库太大,新加入的小伙伴在拉取时,无法切换到最新的分支,报错如下: fetch-pack: unexpected disconnect while reading sideband packet fatal: early EOF fatal: fetch-pack: invalid index-pack output在此记录解决步…...

javafx Dialog无法关闭

// 生成二维码图片String qrCodeText "https://example.com";DialogPane grid new DialogPane();grid.setPadding(new Insets(5));VBox vBox new VBox();vBox.setAlignment(Pos.CENTER);Image qrCodeImage generateQRCodeImage(qrCodeText);ImageView customImag…...

vue3中TCplayer应用

环境win10:vitevue3elementUI 1 安装 npm install tcplayer.js2 使用 <template><div><video id"player-container-id" width"414" height"270" preload"auto" playsinline webkit-playsinline></video>&l…...

算法通关村14关 | 数据流中位数问题

1. 数据流中位数问题 题目 LeetCode295: 中位数是有序列表中间的数&#xff0c;如果列表长度是偶数&#xff0c;中位数是中间两个数的平均值&#xff0c; 例如:[2,3,4]的中位数是3&#xff0c; [2,3]中位数是&#xff08;23&#xff09;/ 2 2.5 设计一个数据结构&#xff1a; …...

工厂模式 与 抽象工厂模式 的区别

工厂模式&#xff1a; // 抽象产品接口 interface Product {void showInfo(); }// 具体产品A class ConcreteProductA implements Product {Overridepublic void showInfo() {System.out.println("This is Product A");} }// 具体产品B class ConcreteProductB impl…...

安装虚拟机+安装/删除镜像

安装虚拟机 注意&#xff0c;官网可能无法登录&#xff0c;导致无法从官网下载&#xff0c;就自己去网上搜靠谱的下载&#xff0c;我用的16.2.3 删除镜像 Vm虚拟机怎么删除已经创建的系统&#xff1f;Vm虚拟机创建好之后iso删除方法 - 系统之家 (xitongzhijia.net) 安装镜像…...

MySQL的内置函数复合查询内外连接

文章目录 内置函数时间函数字符串函数数学函数其他函数 复合查询多表笛卡尔积自连接在where中使用子查询多列子查询在from中使用子查询 内连接外连接左外连接右外连接 内置函数 时间函数 函数描述current_date()当前日期current_time()当前时间current_timestamp()当前时间戳…...

操作系统(OS)与系统进程

操作系统&#xff08;OS&#xff09;与系统进程 冯诺依曼体系结构操作系统(Operator System)进程基本概念进程的描述&#xff08;PCB&#xff09;查看进程通过系统调用获取进程标示符&#xff08;PID&#xff09;通过系统调用创建进程&#xff08;fork&#xff09;进程状态&…...

防重复提交:自定义注解 + 拦截器(HandlerInterceptor)

防重复提交&#xff1a;自定义注解 拦截器&#xff08;HandlerInterceptor&#xff09; 一、思路&#xff1a; 1、首先自定义注解&#xff1b; 2、创建拦截器实现类&#xff08;自定义类名称&#xff09;&#xff0c;拦截器&#xff08;HandlerInterceptor&#xff09;; 3…...

Excel中将文本格式的数值转换为数字

在使用excel时&#xff0c;有时需要对数字列进行各种计算&#xff0c;比如求平均值&#xff0c;我们都知道应该使用AVERAGE()函数&#xff0c;但是很多时候结果却“不尽如人意”。 1 问题&#xff1a; 使用AVERAGE函数&#xff1a; 结果&#xff1a; 可以看到单元格左上角有个…...

uni-app开发小程序中遇到的map地图的点聚合以及polygon划分区域问题

写一篇文章来记录以下我在开发小程序地图过程中遇到的两个小坑吧&#xff0c;一个是点聚合&#xff0c;用的是joinCluster这个指令&#xff0c;另一个是polygon在地图上划分多边形的问题&#xff1a; 1.首先说一下点聚合问题&#xff0c;由于之前没有做过小程序地图问题&#…...

【笔记】软件测试的艺术

软件测试的心理学和经济学 测试是为发现错误而执行程序的过程&#xff0c;所以它是一个破坏性的过程&#xff0c;测试是一个“施虐”的过程。 软件测试的10大原则 1、测试用例需要对预期输出的结果有明确的定义 做这件事的前提是能够提前知晓需求和效果图&#xff0c;如果不…...

配置本地maven

安装maven安装包 修改环境变量 vim ~/.bash_profile export JMETER_HOME/Users/yyyyjinying/apache-jmeter-5.4.1 export GOROOT/usr/local/go export GOPATH/Users/yyyyjinying/demo-file/git/backend/go export GROOVY_HOME/Users/yyyyjinying/sortware/groovy-4.0.14 exp…...

C# 按钮的AcceptButton和CancelButton属性

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System...

SMT贴片制造:专业、现代、智能的未来之选

在现代科技的快速发展下&#xff0c;SMT贴片制造作为电子元器件的核心工艺之一&#xff0c;正以其专业、现代和智能的特点成为未来的首选。 随着电子产品越来越小型化&#xff0c;传统的手工焊接已经无法满足高速、高精度、高稳定性的要求。而SMT贴片制造作为一种先进的表面贴…...

python sqlalchemy db.session 的commit()和colse()对session中的对象的影响

实验一&#xff1a;commit&#xff08;&#xff09;之后查看stu的属性id,查看db.session是否改变 db_test.route("/db_test",methods["GET"]) def db_test():stuStuTest()stu.stu_age22stu.stu_name"nnannns"stu.stu_class11print("sessio…...

ChipDNA PUF技术:从晶体管失配到硬件安全密钥的工程实践

1. 项目概述&#xff1a;当芯片拥有“DNA”&#xff0c;嵌入式安全进入新纪元在嵌入式系统设计领域&#xff0c;安全从来不是一个可以事后弥补的附加功能&#xff0c;而是必须从硬件层面开始构建的基石。随着物联网设备的爆炸式增长&#xff0c;从智能门锁到工业控制器&#xf…...

别再为VectorCAST环境变量头疼了!手把手教你配置.bat启动脚本(附DO-178C等标准切换指南)

VectorCAST启动脚本配置全指南&#xff1a;从环境变量到行业标准切换 第一次双击那个神秘的.bat文件时&#xff0c;我盯着闪退的命令行窗口足足愣了五分钟。作为刚接触航空电子单元测试的嵌入式工程师&#xff0c;VectorCAST的环境配置就像一堵无形的墙——编译器路径报错、环境…...

别再只画区间了!用ECharts的markArea实现单点高亮标注(附完整代码)

突破ECharts标记边界&#xff1a;用markArea实现单点高亮的高级技巧 在数据可视化领域&#xff0c;ECharts凭借其强大的功能和灵活的配置选项&#xff0c;已成为前端开发者和数据分析师的首选工具之一。当我们面对需要突出显示特定数据点的场景时&#xff0c;常规做法是使用mar…...

技术人的职业健康:保护身体,持续前行

技术人的职业健康&#xff1a;保护身体&#xff0c;持续前行 引言 作为一名技术人&#xff0c;我们常常长时间坐在电脑前&#xff0c;忽略了身体健康。今天就来分享一下职业健康的重要性和保护方法。 常见健康问题 颈椎问题 长时间低头看电脑会导致颈椎问题&#xff1a; 症状&a…...

《ROS 2机器人开发从入门到实践》 2.3 使用功能包组织C++节点

简介&#xff1a; 上一小节我们用功能包组织了python节点&#xff0c;这节我们把C节点也装进功能包。 参考资料&#xff1a; 参考资料均来自于鱼香ROS社区创始人小鱼&#xff0c;资源如下&#xff1a; ①&#xff1a;【《ROS 2机器人开发从入门到实践》 2.3 使用功能包组织…...

Qt实战:手把手教你打造一个可动态配置的数值输入组件(基于QDoubleSpinBox封装)

Qt实战&#xff1a;构建可动态配置的数值输入组件的高级封装策略 在复杂的Qt应用开发中&#xff0c;数值输入控件是用户交互的重要组成部分。标准QDoubleSpinBox虽然提供了基础功能&#xff0c;但在实际企业级应用中往往需要更灵活的配置能力和更精细的行为控制。本文将深入探讨…...

ARM弱内存序模型解析:多核并发编程中的内存屏障与同步原语

1. 项目概述&#xff1a;为什么我们需要深入理解ARM的存储一致性模型&#xff1f; 在嵌入式开发、移动计算乃至如今的服务器领域&#xff0c;ARM架构已经无处不在。作为一名长期与底层硬件和操作系统打交道的开发者&#xff0c;我见过太多因对内存模型理解不足而导致的“幽灵”…...

端侧AI算力瓶颈与优化企业格局解析

一、引言&#xff1a;端侧AI的发展困境与研究核心1.1 端侧AI的产业价值与普及现状端侧AI作为边缘计算的核心落地形态&#xff0c;正深度渗透工业制造、智能终端、车载电子、安防监控等领域。据IDC数据&#xff0c;2025年全球端侧AI芯片市场规模突破180亿美元&#xff0c;工业端…...

如何快速搞定GTNH中文汉化:新手友好的终极指南

如何快速搞定GTNH中文汉化&#xff1a;新手友好的终极指南 【免费下载链接】Translation-of-GTNH GTNH整合包的汉化 项目地址: https://gitcode.com/gh_mirrors/tr/Translation-of-GTNH 还在为GTNH&#xff08;GregTech: New Horizons&#xff09;这个顶级整合包的全英文…...

Camera Shakify:Blender相机抖动动画插件深度解析与性能优化指南

Camera Shakify&#xff1a;Blender相机抖动动画插件深度解析与性能优化指南 【免费下载链接】camera_shakify 项目地址: https://gitcode.com/gh_mirrors/ca/camera_shakify 在Blender动画制作中&#xff0c;相机运动的真实性直接影响观众的沉浸感。传统手动关键帧方法…...