Spring事件解析
首先介绍Spring事件相关类的关系:

其中EventListener与EventObject均是Java SE的范畴,源码如下:
package java.util;
public interface EventListener {
}
package java.util;
public class EventObject implements java.io.Serializable {
private static final long serialVersionUID = 5516075349620653480L;
protected transient Object source;
public EventObject(Object source) {
if (source == null)
throw new IllegalArgumentException("null source");
this.source = source;
}
public Object getSource() {
return source;
}
public String toString() {
return getClass().getName() + "[source=" + source + "]";
}
}
Spring继承以上二者,可知Spring的事件实现机制也是基于观察者模式(Observer),除此之外,Spring提供了针对Bean的事件传播功能。
事件机制三元素:发布者、接收者、事件对象;接收者也可称为监听器。Spring中、通过分别继承Java SE中的EventListener和EventObject实现了接收者、事件对象。
package org.springframework.context;
import java.util.EventListener;
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E event);
}
package org.springframework.context;
import java.util.EventObject;
public abstract class ApplicationEvent extends EventObject {
private static final long serialVersionUID = 7099057708183571937L;
private final long timestamp;
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
public final long getTimestamp() {
return this.timestamp;
}
}
Java SE中并未提供事件发布者这一角色,在Spring中,则提供了ApplicationEventPublisher接口作为发布者,源码如下:
package org.springframework.context;
public interface ApplicationEventPublisher {
void publishEvent(ApplicationEvent event);
void publishEvent(Object event);
}
至此、Spring事件机制三大角色全部构建完成。下面通过案例解析,并进一步解说发布者,代码如下:
事件对象:
package com.charles.spring.event;
import org.springframework.context.ApplicationEvent;
public class HelloEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L;
public HelloEvent(Object source) {
super(source);
}
}
接收者/监听器:
package com.charles.spring.event; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; public class HelloListener implements ApplicationListener<ApplicationEvent> { @Override
public void onApplicationEvent(ApplicationEvent event) { if(event instanceof HelloEvent){
System.out.println("Hello World");
} } }
Spring配置文件:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd "> <description>spring-configuration</description> <bean id="helloEvent" class="com.charles.spring.event.HelloEvent">
<constructor-arg index="0" value="Hello" />
</bean>
<bean id="helloListener" class="com.charles.spring.event.HelloListener" /> </beans>
测试代码:
package com.charles.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.charles.spring.event.HelloEvent; public class TestEvent { @Test
@SuppressWarnings("resource")
public void testHelloEvent(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring-config.xml");
HelloEvent helloEvent = (HelloEvent)applicationContext.getBean("helloEvent");
applicationContext.publishEvent(helloEvent);
} }
运行结果:

至此、案例代码结束,也许你尚未看到发布者的实现在哪里,其实在文章开头的类图中就ApplicationEventPublisher的继承实现作了介绍,ApplicationContext接口继承了ApplicationEventPublisher,所以我们在测试代码中,直接使用ApplicationContext作为发布者进行发布。
Spring事件解析的更多相关文章
- Spring事件监听机制源码解析
Spring事件监听器使用 1.Spring事件监听体系包括三个组件:事件.事件监听器,事件广播器. 事件:定义事件类型和事件源,需要继承ApplicationEvent. package com.y ...
- 深入理解Spring事件机制(一):广播器与监听器的初始化
前言 Spring 从 3.x 开始支持事件机制.在 Spring 的事件机制中,我们可以令一个事件类继承 ApplicationEvent 类,然后将实现了 ApplicationListener ...
- Spring源代码解析
Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...
- spring发布和接收定制的事件(spring事件传播)
spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报 分类: 开源技术(如Struts/spring/Hibernat ...
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- Spring源代码解析(收藏)
Spring源代码解析(收藏) Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的 ...
- Spring 使用介绍(十一)—— Spring事件
一.简介 spring事件是观察者设计模式的实现,主要有三个元素: 事件 spring事件由ApplicationEvent定义 监听者 由ApplicationListener定义 发布者 由App ...
- Spring 事件
JDK事件 java通过java.util.EventObject类和java.util.EventListener接口描述事件和监听器 事件源,事件的产生者,任何一个EventObject都必须拥有 ...
- spring事件机制
前置知识补充: 程序里面所谓的“上下文”就是程序的执行环境,打个比方:你就相当于web程序,你的房子就相当于web程序的上下文,你可以在家里放东西,也可以取东西,你的衣食住行都依赖这个房子,这个房子就 ...
随机推荐
- Shiro眼皮下玩ajax,玩出302 Found(实践得经验)
2017/06/14这一天,是我玩Shiro安全框架最刻骨铭心的一天.因为Shiro今天给我深深的补了一刀,在这儿我也给各位补一刀吧,其实问题很简单,解决方式也极其简单,只是给各位分享一下这个错误,纯 ...
- influxdb + Grafana可视化监控平台
在centos6.5上influxdb + Grafana监控平台配置: 1.RedHat and CentOS users can install the latest stable version ...
- java知识点整理
1 java 和Tomcat总结 脑图地址 (其中web 容器部分还需要继续完善,但是没找到相关文档) 跟着java Se 文档梳理了一下学习路线图(方便全面掌握要点,及时对自己查漏补缺),以及一些 ...
- 在firefox的flashgot中配置各种下载器
一.在firefox中安装flashgot下载管理器 flashgot是firefox的一个扩展,在联网的情况下,可以在firefox中的附加组件中搜索flashgot,然后安装. 二.在flashg ...
- /dev/shm 与 tmpfs
1./dev/shm 与 tmpfs /dev/shm/是linux下一个目录,/dev/shm目录不在磁盘上,而是在内存里, 类型为 tmpfs ,因此使用linux /dev/shm/ 的效率非常 ...
- CSS 中的内联元素、块级元素、display的各个属性的特点
CSS的内联元素和块级元素 块级元素<h1>-<h6>.p.dt是不可以内联块级元素的 1.block和inline这两个概念是简略的说法,完整确切的说应该是 block-le ...
- CoolBlog开发笔记第3课:创建Django应用
教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 前言 经过上一节我们已经创建了CoolBlog工程,但是关于CoolBlog的功能代码 ...
- Python进阶-继承中的MRO与super
Python进阶-继承中的MRO与super 写在前面 如非特别说明,下文均基于Python3 摘要 本文讲述Python继承关系中如何通过super()调用"父类"方法,supe ...
- Python Click 学习笔记(转)
原文链接:Python Click 学习笔记 Click 是 Flask 的团队 pallets 开发的优秀开源项目,它为命令行工具的开发封装了大量方法,使开发者只需要专注于功能实现.恰好我最近在开发 ...
- 11.巨坑,注意了,关于显示不正常的问题,localstorage的存储问题
在存储时,localstorage和sessionstorage只能存储字符串,所以,必须把json转换为字符串再存,JSON.stringify