本文介绍下Spring中的事件监听,其本质也就是观察者模型(发布/订阅模式),具体的观察者模式参考下文


Java观察者模式(Observer)


@

Spring事件监听

一、事件监听案例

1.事件类

/**
* 事件类
* @author 波波烤鸭
* @email dengpbs@163.com
*
*/
public class MyEvent extends ApplicationContextEvent { private static final long serialVersionUID = 1L; public MyEvent(ApplicationContext source) {
super(source);
System.out.println("myEvent 构造方法被执行了...");
} public void out(String name){
System.out.println("myEvent .... out方法执行了"+name);
}
}

2.事件监听类

  事件监听器也就是我们的观察者。我们可以创建多个来观察。

/**
* 监听器
* 观察者
* @author 波波烤鸭
* @email dengpbs@163.com
*
*/
public class MyListenerA implements ApplicationListener<MyEvent>{ @Override
public void onApplicationEvent(MyEvent event) {
System.out.println("MyListenerA 监听器触发了...");
// 执行事件中的特定方法
event.out("AAAAA");
}
}
/**
* 监听器
* 观察者
* @author 波波烤鸭
* @email dengpbs@163.com
*
*/
public class MyListenerB implements ApplicationListener<MyEvent>{ @Override
public void onApplicationEvent(MyEvent event) {
System.out.println("MyListenerB 监听器触发了...");
// 执行事件中的特定方法
event.out("BBBBB");
}
}

3.事件发布者

/**
* 事件发布类
* 实现ApplicationContextAware接口用来感知ApplicationContext对象
* @author 波波烤鸭
* @email dengpbs@163.com
*
*/
public class MyPublisher implements ApplicationContextAware{ public ApplicationContext ac; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.ac = applicationContext;
}
/**
* 发布事件
* 监听该事件的监听者都可以获取消息
* @param event
*/
public void publisherEvent(ApplicationEvent event){
System.out.println("---发布事件---"+event);
ac.publishEvent(event);
}
}

4.配置文件中注册

<?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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:annotation-config/> <bean class="com.dpb.pojo.User" id="user" >
<property name="name" value="波波烤鸭"></property>
</bean> <!-- 注册事件类 -->
<bean class="com.dpb.event.MyEvent"></bean> <!-- 注册监听器 -->
<bean class="com.dpb.listener.MyListenerA"></bean>
<bean class="com.dpb.listener.MyListenerB"></bean> <!-- 注册发布者类 -->
<bean class="com.dpb.publisher.MyPublisher"></bean>
</beans>

5.测试

@Test
public void test1() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从Spring容器中获取发布者
MyPublisher bean = ac.getBean(MyPublisher.class);
// 从Spring容器中获取事件对象
MyEvent event = ac.getBean(MyEvent.class);
// 发布者发布事件
bean.publisherEvent(event);
}

输出结果

myEvent 构造方法被执行了...
---发布事件---com.dpb.event.MyEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@311d617d: startup date [Wed Mar 06 13:04:57 CST 2019]; root of context hierarchy]
MyListenerA 监听器触发了...
myEvent .... out方法执行了AAAAA
MyListenerB 监听器触发了...
myEvent .... out方法执行了BBBBB

小结:通过案例我们实现了事件发生后注册的有此事件的监听者(观察者)监听到了此事件,并做出了响应的处理。

二、Spring中事件监听分析

1. Spring中事件监听的结构

2. 核心角色介绍

2.1 ApplicationEvent

  ApplicationEvent是所有事件对象的父类。ApplicationEvent继承自jdk的EventObject,所有的事件都需要继承ApplicationEvent,并且通过source得到事件源。

public abstract class ApplicationEvent extends EventObject {

	/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L; /** System time when the event happened */
private final long timestamp; /**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}

实现类:

2.2 ApplicationListener

  ApplicationListener事件监听器,也就是观察者。继承自jdk的EventListener,该类中只有一个方法onApplicationEvent。当监听的事件发生后该方法会被执行。

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}

实现类

2.3 ApplicationContext

  ApplicationContext是Spring中的核心容器,在事件监听中ApplicationContext可以作为事件的发布者,也就是事件源。因为ApplicationContext继承自ApplicationEventPublisher。在ApplicationEventPublisher中定义了事件发布的方法

public interface ApplicationEventPublisher {

	/**
* Notify all <strong>matching</strong> listeners registered with this
* application of an application event. Events may be framework events
* (such as RequestHandledEvent) or application-specific events.
* @param event the event to publish
* @see org.springframework.web.context.support.RequestHandledEvent
*/
void publishEvent(ApplicationEvent event); /**
* Notify all <strong>matching</strong> listeners registered with this
* application of an event.
* <p>If the specified {@code event} is not an {@link ApplicationEvent},
* it is wrapped in a {@link PayloadApplicationEvent}.
* @param event the event to publish
* @since 4.2
* @see PayloadApplicationEvent
*/
void publishEvent(Object event); }



具体发布消息的方法实现:AbstractApplicationContext中

protected void publishEvent(Object event, ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
} // Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<Object>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
}
} // Multicast right now if possible - or lazily once the multicaster is initialized
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
} // Publish event via parent context as well...
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}

getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);这行代码的作用是获取ApplicationEventMulticaster来广播事件给所有的监听器。

2.4 ApplicationEventMulticaster

  事件广播器,它的作用是把Applicationcontext发布的Event广播给所有的监听器.



具体的注册监听是在AbstractApplicationContext中实现的。

@Override
public void addApplicationListener(ApplicationListener<?> listener) {
Assert.notNull(listener, "ApplicationListener must not be null");
if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener);
}
else {
this.applicationListeners.add(listener);
}
}

三、总结

  1. Spring中的事件监听使用的是观察者模式
  2. 所有事件需要继承ApplicationEvent父类
  3. 所有的监听器需要实现ApplicationListener接口
  4. 事件发布需要通过ApplicationContext中的publisherEvent方法实现
  5. 监听器的注册是ApplicationEventMulticaster提供的,但我们并不需要实现。

Spring之事件监听(观察者模型)的更多相关文章

  1. Spring的事件监听机制

    最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...

  2. 十一、Spring之事件监听

    Spring之事件监听 ApplicationListener ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成Applica ...

  3. Spring的事件监听ApplicationListener

    ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...

  4. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  5. Spring ApplicationContext(八)事件监听机制

    Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...

  6. spring事件监听(eventListener)

    原理:观察者模式 spring的事件监听有三个部分组成,事件(ApplicationEvent).监听器(ApplicationListener)和事件发布操作. 事件 事件类需要继承Applicat ...

  7. tomcat的事件监听

    //事件涉及的三个组件:事件源.事件对象.事件监听器 //一个总的事件监听器接口,所有不同分类的事件监听器都继承这个接口 public interface EventListener {} //例如  ...

  8. java 事件监听

    事件监听实现: 三要素: 1.事件源(数据源,要处理的数据) 2.事件 (承载数据,传递信息并被监听) 3.监听器 (负责对数据的业务处理) --该开发用例采用了Spring的事件监听 1.  定义事 ...

  9. Spring 事件监听机制及原理分析

    简介 在JAVA体系中,有支持实现事件监听机制,在Spring 中也专门提供了一套事件机制的接口,方便我们实现.比如我们可以实现当用户注册后,给他发送一封邮件告诉他注册成功的一些信息,比如用户订阅的主 ...

随机推荐

  1. Egret的按钮事件处理

    首先要在exml内要设置有对应按钮的ID 2,编写TypeScript脚本: public mybutton:eui.Button; 函数内部:this.mybutton.addEventListen ...

  2. Java-IO 字节流的使用和效率比较

    打算做一个系列,前面讲了基本的字符流的用法,这篇博客介绍一下字节流的基本用法: 一.基本使用: 基本字节流: FileInputStream   FileOutputStream BufferedIn ...

  3. java课程之团队开发冲刺阶段1.1

    一.今天所要完成的内容 1.实现软件添加日期的功能并生成当前所在周的功能 2.对之前的代码进行重新排版,将主函数的内容移到方法中 3.利用Android自带的左侧菜单栏实现app的整体美观

  4. XML语言1.简介和语法

    一.什么是XML语言? XML 指可扩展标记语言(Extensible Markup Language) Xml是独立于软件和硬件的信息传输工具. XML 是一种很像HTML的标记语言. 但xml不是 ...

  5. Session Cookie介绍和使用

    Cookie机制 Cookie机制 Cookie是服务器存储在本地计算机上的小块文本,并随每个请求发送到同一服务器. IETF RFC 2965 HTTP状态管理机制是一种通用的cookie规范. W ...

  6. 线程(Thread、ThreadPool)

    多线程的操作,推荐使用线程池线程而非新建线程.因为就算只是单纯的新建一个线程,这个线程什么事情也不做,都大约需要1M的内存空间来存储执行上下文数据结构,并且线程的创建与回收也需要消耗资源,耗费时间.而 ...

  7. Oracle分析函数——函数列表

    --------------聚合函数 SUM :该函数计算组中表达式的累积和 MIN :在一个组中的数据窗口中查找表达式的最小值 MAX :在一个组中的数据窗口中查找表达式的最大值 AVG :用于计算 ...

  8. 巧妙设置Texture Type,将ShadowMask内存占用变成之前的1/4

    0x00 前言 在这篇文章中,我选择了过去一周Unity官方社区交流群中比较有代表性的几个问题,总结在这里和大家进行分享.同时,也欢迎大家加入我们这个讨论干货的官方技术群,交流看法分享经验. Unit ...

  9. web项目 easyui-datagrid开发实践

    一,引言 工作的需要,做了3年的wpf(mvvm)……,又因为项目的需求,回归到web的开发. ■  3 years ago,vue是我三年前没用过的玩意儿. ■  3 years ago,boots ...

  10. JDK设计模式之—动态代理

    代理模式的特点 代理模式是常用的java设计模式,它的特征是代理类与委托类有同样的接口.代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类. 代理类的对象并不是真正实现服务,而是通过调用委 ...