当前位置: 首页 > 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…...

手游刚开服就被攻击怎么办?如何防御DDoS?

开服初期是手游最脆弱的阶段&#xff0c;极易成为DDoS攻击的目标。一旦遭遇攻击&#xff0c;可能导致服务器瘫痪、玩家流失&#xff0c;甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案&#xff0c;帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...

Git 3天2K星标:Datawhale 的 Happy-LLM 项目介绍(附教程)

引言 在人工智能飞速发展的今天&#xff0c;大语言模型&#xff08;Large Language Models, LLMs&#xff09;已成为技术领域的焦点。从智能写作到代码生成&#xff0c;LLM 的应用场景不断扩展&#xff0c;深刻改变了我们的工作和生活方式。然而&#xff0c;理解这些模型的内部…...

MyBatis中关于缓存的理解

MyBatis缓存 MyBatis系统当中默认定义两级缓存&#xff1a;一级缓存、二级缓存 默认情况下&#xff0c;只有一级缓存开启&#xff08;sqlSession级别的缓存&#xff09;二级缓存需要手动开启配置&#xff0c;需要局域namespace级别的缓存 一级缓存&#xff08;本地缓存&#…...

Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践

在 Kubernetes 集群中&#xff0c;如何在保障应用高可用的同时有效地管理资源&#xff0c;一直是运维人员和开发者关注的重点。随着微服务架构的普及&#xff0c;集群内各个服务的负载波动日趋明显&#xff0c;传统的手动扩缩容方式已无法满足实时性和弹性需求。 Cluster Auto…...

聚六亚甲基单胍盐酸盐市场深度解析:现状、挑战与机遇

根据 QYResearch 发布的市场报告显示&#xff0c;全球市场规模预计在 2031 年达到 9848 万美元&#xff0c;2025 - 2031 年期间年复合增长率&#xff08;CAGR&#xff09;为 3.7%。在竞争格局上&#xff0c;市场集中度较高&#xff0c;2024 年全球前十强厂商占据约 74.0% 的市场…...