Spring事件的组件

主要是3个组件:

1.ApplicationEvent   事件

2.ApplicationListener 监听器,对事件进行监听

3.ApplicationEventMulticaster 事件广播器,将publish的事件广播给所有的监听器。

事件

ContextRefreshedEvent :当ApplicationContext初始化或者刷新,将会发布,例如使用ConfigurableApplicationContext接口调用refresh方法,初始化意味着加载所有的bean,同时

ApplicationContext是支持热刷新。

ContextStartedEvent:当ApplicationContext启动的时候,将会调用start方法,发布此事件。

ContextStoppedEvent:当容器停止的时候,发布事件。

ContextClosedEvent:当容器关闭的时候,发布事件。

RequestHandledEvent:http请求完成后,发布事件。

监听器

ApplicationListener:ApplicationContext容器内部自定义事件监听器接口,继承自java.util.EventListener,ApplicationContext容器在启动时,会自动识别并加载EventListener类型bean的定义,一旦容器事件发布,将会通知注册到容器的监听器。

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

	/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event); }

发布事件者

ApplicationEventPublisher和ApplicationEventMulticaster
ApplicationEventPublisher:是一个封装事件发布接口,作为ApplicationContext父类接口。
ApplicationEventMulticaster:管理ApplicationListener对象,并且发布它们。

                           

ApplicationContext继承ApplicationEventPublisher而担当者事件发布的角色,而ApplicationContext并没有进行事件的发布,而是交给了AbstractApplicationEventMulticaster来实现事件监听器的管理。

简单实例

模拟人民银行利息调整,通知到其他银行的事件。

利息事件

package org.lzyer.test;

import org.springframework.context.ApplicationEvent;

/**
* Created by Administrator on 2017/10/23.
*/
public class ExRateChangeEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the component that published the event (never {@code null})
*/
public ExRateChangeEvent(Object source) {
super(source);
}
}

农业银行监听器

package org.lzyer.test;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* Created by Administrator on 2017/10/23.
*/
@Component
public class ABCListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ExRateChangeEvent){
System.out.println("农业银行收到->"+event.getSource());
}
}
}

工商银行监听器

package org.lzyer.test;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* Created by Administrator on 2017/10/23.
*/
@Component
public class ICBCListener implements ApplicationListener<ApplicationEvent> { @Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ExRateChangeEvent){
System.out.println("工商银行收到->"+event.getSource());
}
}
}

spring-beans.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.lzyer.test"></context:component-scan>
</beans>

测试代码

@Test
public void testPublishEvent(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-beans.xml");
context.publishEvent(new ExRateChangeEvent("利息上调"));
}

结果:

农业银行收到->利息上调
工商银行收到:->利息上调

事件原理流程

spring容器初始化调用的refresh方法

AbstractApplicationContext@refresh

@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//省略 try {
//省略 // 初始化广播事件
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
onRefresh();
//注册监听器
// Check for listener beans and register them.
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
//省略
}
}
}

initApplicationEventMulticaster()注册广播事件

protected void initApplicationEventMulticaster() {
//获取beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {//创建一个默认的事件广播器
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
"': using default [" + this.applicationEventMulticaster + "]");
}
}
}
AbstractApplicationEventMulticaster类中的属性

registerListeners方法
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String lisName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(lisName);
}
}

在refresh最后的一个方法,中发布刷新事件。

finishRefresh
protected void finishRefresh() {
// Initialize lifecycle processor for this context.
initLifecycleProcessor(); // Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh(); // Publish the final event.发布刷新事件
publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}

事件整体结构图

最后走一波debug

 

参考资料
http://www.jianshu.com/p/bc1055b065ce
http://liuxiamai.iteye.com/blog/2322197
http://jinnianshilongnian.iteye.com/blog/1902886

  


Spring源码解析-事件的更多相关文章

  1. Spring源码解析 - AbstractBeanFactory 实现接口与父类分析

    我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...

  2. spring 源码解析

    1. [文件] spring源码.txt ~ 15B     下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB     下载( ...

  3. Spring源码解析——循环依赖的解决方案

    一.前言 承接<Spring源码解析--创建bean>.<Spring源码解析--创建bean的实例>,我们今天接着聊聊,循环依赖的解决方案,即创建bean的ObjectFac ...

  4. Spring源码解析-ioc容器的设计

    Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...

  5. Spring源码解析系列汇总

    相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的Spring源码解析系列文章的汇总,总共包含以下专题.喜欢的同学可以收藏起来以备不时之需 SpringIOC源码解析(上) 本篇文章搭建了IOC源 ...

  6. Spring源码解析之PropertyPlaceholderHelper(占位符解析器)

    Spring源码解析之PropertyPlaceholderHelper(占位符解析器) https://blog.csdn.net/weixin_39471249/article/details/7 ...

  7. Spring源码解析之BeanFactoryPostProcessor(三)

    在上一章中笔者介绍了refresh()的<1>处是如何获取beanFactory对象,下面我们要来学习refresh()方法的<2>处是如何调用invokeBeanFactor ...

  8. Spring源码解析之ConfigurationClassPostProcessor(二)

    上一个章节,笔者向大家介绍了spring是如何来过滤配置类的,下面我们来看看在过滤出配置类后,spring是如何来解析配置类的.首先过滤出来的配置类会存放在configCandidates列表, 在代 ...

  9. Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean

    Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...

随机推荐

  1. eclipse 右键没有Build Path

    如果Project Explorer右键没有build pathWindow ->show view 选择package explorer 参考https://blog.csdn.net/cod ...

  2. Verilog学习笔记基本语法篇(七)········ 生成块

    生成块可以动态的生成Verilog代码.可以用于对矢量中的多个位进行重复操作.多个模块的实例引用的重复操作.根据参数确定程序中是否包含某段代码.生成语句可以控制变量的声明.任务和函数的调用.还能对实例 ...

  3. codeforces 845A Chess Tourney

    参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...

  4. 最短路径算法 2.Dijkstra算法

    Dijkstra 算法解决的是带权重的有向图上单源最短路径问题,该算法要求所有边的权重都为非负值.该算法的时间复杂度是O(N2),相比于处理无负权的图时,比Bellmad-Ford算法效率更高. 算法 ...

  5. 剑指offer题目系列二

    本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...

  6. FireDAC 连接Access (accdb)数据库

    FireDAC可以方便连接数据库,但是要连接新版本的accdb数据库,要注意这样的事项(以Office2010版为例) 安装Office2010 x86版,注意,不能安装x64版,因为Delphi I ...

  7. VI的配置

    vi下设置tab键为4个空格 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或".exrc",没有的可以新建一个.用户可以编辑它,使这些设置在 ...

  8. **leetcode笔记--4 Sum of Two Integers

    question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + a ...

  9. C#操作Excel文件(转)

    摘要:本文介绍了Excel对象.C#中的受管代码和非受管代码,并介绍了COM组件在.net环境中的使用. 关键词:受管代码:非受管代码:Excel对象:动态连接库 引言 Excel是微软公司办公自动化 ...

  10. react实现换肤功能

    一.目标   提供几种主题色给用户选择,然后根据用户的选择改变应用的主题色: 二.实现原理   1.准备不同主题色的样式文件:   2.将用户的选择记录在本地缓存中:   3.每次进入应用时,读取缓存 ...