Spring之事件发布系统
springboot应用,启动spring容器大致有如下几个过程:
- 容器开始启动
- 初始化环境变量
- 初始化上下文
- 加载上下文
- 完成
对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下:
public interface SpringApplicationRunListener { /**
* Called immediately when the run method has first started. Can be used for very
* early initialization.
*/
void started(); /**
* Called once the environment has been prepared, but before the
* {@link ApplicationContext} has been created.
* @param environment the environment
*/
void environmentPrepared(ConfigurableEnvironment environment); /**
* Called once the {@link ApplicationContext} has been created and prepared, but
* before sources have been loaded.
* @param context the application context
*/
void contextPrepared(ConfigurableApplicationContext context); /**
* Called once the application context has been loaded but before it has been
* refreshed.
* @param context the application context
*/
void contextLoaded(ConfigurableApplicationContext context); /**
* Called immediately before the run method finishes.
* @param context the application context or null if a failure occurred before the
* context was created
* @param exception any run exception or null if run completed successfully.
*/
void finished(ConfigurableApplicationContext context, Throwable exception); }
监听器
对应几个关键事件
监听器接口中的每个方法代表容器启动过程一个阶段,每一个阶段可以自定义执行一系列任务。可以将SpringApplicationRunListener理解成一个命令模式(传入一个对象,至于当前这个时间点,如何调用,不关心)。每一个阶段执行的序列图如下所示,其中任意方法,为SpringApplicationRunListener接口中定义的,此阶段调用的方法。
SpringApplicationRunListeners是对多个SpringApplicationRunListener做了一次代理,聚合多个SpringApplicationRunListener,在任一个过程完成,会依次调用对应方法。
private final List<SpringApplicationRunListener> listeners; SpringApplicationRunListeners(Log log,
Collection<? extends SpringApplicationRunListener> listeners) {
this.log = log;
this.listeners = new ArrayList<SpringApplicationRunListener>(listeners);
} public void started() {
for (SpringApplicationRunListener listener : this.listeners) {
listener.started();
}
} public void environmentPrepared(ConfigurableEnvironment environment) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}
SpringApplicationRunListener有一个特殊的实现EventPublishingRunListener,他的主要功能就是,在特定时间点出发特殊事件。
@Override
public void started() {
this.initialMulticaster.multicastEvent(new ApplicationStartedEvent(
this.application, this.args));
} @Override
public void environmentPrepared(ConfigurableEnvironment environment) {
this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(
this.application, this.args, environment));
}
事件发布采用观察者模式,整个事件发布流程如下图。
Spring之事件发布系统的更多相关文章
- Spring的事件发布机制
一:Spring的事件发布 ApplicationContext提供了针对Bean的事件传播功能,其中的主角是publishEvent()方法,通过这个方法可以将事件通知给系统内的监听器(需实现App ...
- 【spring源码学习】spring的事件发布监听机制源码解析
[一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...
- spring 自定义事件发布及监听(简单实例)
前言: Spring的AppilcaitionContext能够发布事件和注册相对应的事件监听器,因此,它有一套完整的事件发布和监听机制. 流程分析: 在一个完整的事件体系中,除了事件和监听器以外,还 ...
- spring boot 事件发布与接收
1.创建发布对象 LoginEvent 2.在要发布对象的地方注入 ApplicationEventPublisher @Autowired ApplicationEventPublisher pub ...
- Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制
上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...
- Spring的事件监听机制
最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...
- 十一、Spring之事件监听
Spring之事件监听 ApplicationListener ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成Applica ...
- 深入理解Spring的容器内事件发布监听机制
目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...
- 从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码
前言 最近在项目中做了一项优化,对业务代码进行解耦.我们部门做的是警用系统,通俗的说,可理解为110报警.一条警情,会先后经过接警员.处警调度员.一线警员,警情是需要记录每一步的日志,是要可追溯的,比 ...
随机推荐
- wcf session开启
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] pu ...
- 第二次作业#include <stdio.h> int main() { int a,b,c,d,e; printf("请输入一个不多于五位的整数:\n"); scanf("%d",&a); if(a>=100000||a<=0) { printf("输入格式错误! \n"); } else { if(
1 判断成绩等级 给定一百分制成绩,要求输出成绩的等级.90以上为A,80-89为B,70-79为C,60-69为D,60分以下为E,输入大于100或小于0时输出"输入数据错误". ...
- CSS3--背景颜色的渐变效果
一.渐变:gradients 线性渐变(linear gradients) background:-webkit-linear-gradient(90deg,red 10%,blue); 1.需要有内 ...
- locutus(phpjs) 的使用
今天来介绍一个js的框架,这个框架的主要功能呢,是通过加载该类库,来实现php函数的调用 当然了,这并不是说php中所有的函数都能在js中使用,但很大一部分是可以的. 环境:mac + node v5 ...
- unity3d 游戏插件 溶解特效插件 - Dissolve Shader
unity3d 游戏插件 溶解特效插件 - Dissolve Shader 链接: https://pan.baidu.com/s/1hr7w39U 密码: 3ed2
- Android中轴旋转特效实现,制作别样的图片浏览器
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例 ...
- JS无缝滚动
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- 如何利用java得到当前的时间和前一天的时间
1.得到当前的时间 Calendar cal = Calendar.getInstance(); String time=formatter.format(cal.getTime()); ...
- 关于最近在做的一个js全屏轮播插件
最近去面试了,对方要求我在一个星期内用原生的js代码写一个全屏轮播的插件,第一想法就是跟照片轮播很相似,只是照片轮播是有定义一个宽高度大小已经确定了的容器用来存储所有照片,然后将照片全部左浮动,利用m ...
- delphi常用快捷键(我自己经常使用的)
代码编辑器: Home 回到当前行的头部 End 回到当前行的尾部 Insert 插入代码,覆盖后面的代码,(按回车无效), 再按撤回效果 Delete 删除 F1 双击一个单词后,按F1调用自带的L ...