首先介绍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事件解析的更多相关文章

  1. Spring事件监听机制源码解析

    Spring事件监听器使用 1.Spring事件监听体系包括三个组件:事件.事件监听器,事件广播器. 事件:定义事件类型和事件源,需要继承ApplicationEvent. package com.y ...

  2. 深入理解Spring事件机制(一):广播器与监听器的初始化

    前言 Spring 从 3.x 开始支持事件机制.在 Spring 的事件机制中,我们可以令一个事件类继承 ApplicationEvent 类,然后将实现了 ApplicationListener ...

  3. Spring源代码解析

    Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...

  4. spring发布和接收定制的事件(spring事件传播)

    spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报  分类: 开源技术(如Struts/spring/Hibernat ...

  5. spring 事件(Application Event)

    spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...

  6. Spring源代码解析(收藏)

    Spring源代码解析(收藏)   Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的 ...

  7. Spring 使用介绍(十一)—— Spring事件

    一.简介 spring事件是观察者设计模式的实现,主要有三个元素: 事件 spring事件由ApplicationEvent定义 监听者 由ApplicationListener定义 发布者 由App ...

  8. Spring 事件

    JDK事件 java通过java.util.EventObject类和java.util.EventListener接口描述事件和监听器 事件源,事件的产生者,任何一个EventObject都必须拥有 ...

  9. spring事件机制

    前置知识补充: 程序里面所谓的“上下文”就是程序的执行环境,打个比方:你就相当于web程序,你的房子就相当于web程序的上下文,你可以在家里放东西,也可以取东西,你的衣食住行都依赖这个房子,这个房子就 ...

随机推荐

  1. Work Time Manager【开源项目】- 创建自己日志组件 2.0重构

    Hello all , 我又回来了 这次我们真是开始来聊聊开源项目里,小而有用的模块或者组件的开发思想. 同时,软件已经更新到1.60的版本了,支持新用户注册,可以不再使用统一的test账户了. 您可 ...

  2. 依赖注入之Autofac使用总结

    依赖倒置?控制反转(IOC)? 依赖注入(DI)? 你是否还在被这些名词所困扰,是否看了大量理论文章后还是一知半解了? 今天我想结合实际项目,和正在迷惑中的新手朋友一起来学习和总结依赖注入Autofa ...

  3. SQL中的cast()函数

    CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型.CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型. 语法: CAST (expression AS dat ...

  4. Treasure Hunt

    Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. SDS 链表

    sds定义 struct sdshdr{ int len int free char buf[] } sds和c语言类似,仍然把字符串的末尾加上一个'.0',但是不会计入总长度,也就是不会对len造成 ...

  6. 关于MATLAB处理大数据坐标文件2017620

    暑假已至,接下来组内成员将会各回各家,各找各妈,这肯定是对本次大数据比赛是很不利的. 接下来我会把任务分配给组员,当然任务会比起初的时候轻一点,因为我认为本次比赛的目的并不是我要求组员做什么,而是我的 ...

  7. CSS active选择器与CSS hover选择器

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  8. JQuery实现tab切换

    JQuery实现tab切换: (jquery需要自己添加) <!DOCTYPE html> <html lang="en"> <head> &l ...

  9. 13.如何生成订单号,用uuid

    String orderNum = UUID.randomUUID().toString().replaceAll("-", "");

  10. Openfire插件开发图解

    概述 Openfire插件开发是Openfire的精髓之一,支持插件热插拔,还可以方便的在web端进行管理插件.插件分为两种,一种是以服务为主的控制台插件,一种是包括页面或对外开放Servlet接口. ...