SpringBoot--启动时的事件机制
原文网址:SpringBoot--启动时的事件机制_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍SpringBoot启动过程中的事件机制。
SpringBoot的启动过程用到了事件监听机制,它属于观察者模式。学习事件监听机制可以让我们更好地理解SpringBoot的启动流程,也有利于将观察者模式应用于项目。
系列文章
- SpringBoot--自定义事件--方法/详解/实例_IT利刃出鞘的博客-CSDN博客
- SpringBoot--自定义事件--同步与异步_IT利刃出鞘的博客-CSDN博客_springboot监听器的原理和用途
- SpringBoot--启动时的事件机制_IT利刃出鞘的博客-CSDN博客
SpringBoot启动过程的事件
启动事件(按先后顺序)
- ApplicationStartingEvent
- 这个事件在 Spring Boot 应用运行开始时,且进行任何处理之前发送(除了监听器和初始化器注册之外)。
- ApplicationEnvironmentPreparedEvent
- 这个事件在当已知要在上下文中使用 Spring 环境(Environment)时,在 Spring 上下文(context)创建之前发送。
- ApplicationContextInitializedEvent
- 这个事件在当 Spring 应用上下文(ApplicationContext)准备好了,并且应用初始化器(ApplicationContextInitializers)已经被调用,在 bean 的定义(bean definitions)被加载之前发送。
- ApplicationPreparedEvent
- 这个事件是在 Spring 上下文(context)刷新之前,且在 bean 的定义(bean definitions)被加载之后发送。
- ApplicationStartedEvent
- 这个事件是在 Spring 上下文(context)刷新之后,且在 application/ command-line runners 被调用之前发送。
- AvailabilityChangeEvent
- 这个事件紧随上个事件之后发送,状态:ReadinessState.CORRECT,表示应用已处于活动状态。
- ApplicationReadyEvent
- 这个事件在任何 application/ command-line runners 调用之后发送。
- AvailabilityChangeEvent
- 这个事件紧随上个事件之后发送,状态:ReadinessState.ACCEPTING_TRAFFIC,表示应用可以开始准备接收请求了。
- ApplicationFailedEvent
- 这个事件在应用启动异常时进行发送。
上面所介绍的这些事件列表仅包括绑定到 SpringApplication 的 SpringApplicationEvents 事件,除了这些事件以外,以下事件也会在 ApplicationPreparedEvent 之后和 ApplicationStartedEvent 之前发送:
- WebServerInitializedEvent
- 这个 Web 服务器初始化事件在 WebServer 启动之后发送,对应的还有 ServletWebServerInitializedEvent(Servlet Web 服务器初始化事件)、ReactiveWebServerInitializedEvent(响应式 Web 服务器初始化事件)。
- ContextRefreshedEvent
- 这个上下文刷新事件是在 Spring 应用上下文(ApplicationContext)刷新之后发送。
原理
源码分析
myPublisher.myPublish("Hello") //HelloController
applicationContext.publishEvent(new MyEvent(message)); //MyPublisher
publishEvent(event, (ResolvableType)null); //AbstractApplicationContext.class
// AbstractApplicationContext
getApplicationEventMulticaster().multicastEvent((ApplicationEvent)applicationEvent, eventType);
// AbstractApplicationContext
applicationEventMulticaster.multicastEvent((ApplicationEvent)applicationEvent, eventType)
SimpleApplicationEventMulticaster#multicastEvent
以下都在SimpleApplicationEventMulticaster
multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType)
// 此时,type为:com.example.tmp.MyEvent
ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
for (ApplicationListener<?> listener : getApplicationListeners(event, type))
invokeListener(listener, event)
AbstractApplicationEventMulticaster#getApplicationListeners
getApplicationListeners(event, type))
retriever = new ListenerRetriever(true);
Collection<ApplicationListener<?>> listeners = retrieveApplicationListeners(eventType, sourceType, retriever);
listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
遍历listeners,如果监听器支持此事件,则加入集合并返回。
其他网址
Spring Boot 启动事件和监听器,太强大了!_Java技术栈,分享最主流的Java技术-CSDN博客
SpringBoot--启动时的事件机制相关推荐
- 启动rrt什么意思_面试官:你来说一下springboot启动时的一个自动装配过程吧!...
前言 继续总结吧,没有面试就继续夯实自己的基础,前阵子的在面试过程中遇到的各种问题陆陆续续都会总结出来分享给大家,这次要说的也是面试中被问到的一个高频的问题,我当时其实没答好,因为很早之前是看过spr ...
- SpringBoot启动时就会自动去连接mongdo DB指向的url
SpringBoot启动时就会去连接mongdo DB指向的url:
- Springboot 启动时Bean初始化,启动异常-Assert.isTrue(condition,message) 报错
Springboot 启动时Bean初始化启动异常Assert.isTrue(condition,message) 报错,如果 condition为false 则会出现 java.lang.Illeg ...
- SpringBoot启动时 提示没有主清单属性 MANIFEST
SpringBoot启动时 提示没有主清单属性 MANIFEST <?xml version="1.0" encoding="UTF-8"?>< ...
- SpringBoot启动发生的事件顺序
Springboot启动过程发生事件 ApplicationStartingEvent 应用开始启动 ApplicationEnvironmentPreparedEvent:环境准备完成(contex ...
- SpringBoot启动时将数据库数据加载到内存
文章目录 为什么需要将参数存入缓存?以及选择何种方式 SpringBoot启动时将数据库参数表参数加载到内存 常见思路 思路1: 使用@PostConstruct 思路2: 实现CommandLine ...
- springboot启动时控制台打印图案
springboot启动时控制台打印图案如下: 1.假如我们不想看到这个图案 public static void main(String[] args) {SpringApplication app ...
- 如何在SpringWEB项目或者Springboot启动时直接执行业务代码(三种方式)
我的博客原文链接 前言 通常的我们的项目开发中,经常会遇到那种在服务一启动就需要自动执行一些业务代码的情况.比如将数据库中的配置信息或者数据字典之类的缓存到redis,或者在服务启动的时候将一些配置化 ...
- SpringBoot 启动时的运行方法
文章目录 为什么我们需要在 spring boot 启动时运行代码? spring boot中启动后不同的运行方式 1.使用CommandLineRunner接口 实现 CommandLineRunn ...
最新文章
- 管道符、shell变量、环境变量配置文件
- (软件工程复习核心重点)第二章可行性研究习题
- 究竟如何保证session一致性?
- 黑马博客——详细步骤(一)路由跳转和抽取公共部分代码
- 基于单片机c语言的光立方,基于单片机的光立方设计(课程设计).doc
- VS2015开发工具里面没有Arial字体怎么办?VS如何添加字体
- FDDB数据集(人脸检测)
- Oracle优化器详解,通宵整理,透彻超详细
- 未能成功加载扩展程序_在手机浏览器上安装Chrome扩展的方法,你们都用上了吗?...
- winpe装双系统linux_自制WINPE+MAC安装U盘及双系统存储U盘(增加多系统安装)
- 推荐一个好组件Javascript文本比较工具
- ArcGis空间分析学习:超市选址分析
- 数据挖掘——无量纲化
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- IB1010音频解码芯片
- Spring4详解系列(一)Spring之旅
- 制药实验室信息管理系统(LIMS)
- RK CPU调试技巧
- fck编辑器的使用(经典)
- 电子器件系列29:n型mos管选型时应注意哪些技术参数