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


Java观察者模式(Observer)


@

Spring事件监听

一、事件监听案例

1.事件类

  1. /**
  2. * 事件类
  3. * @author 波波烤鸭
  4. * @email dengpbs@163.com
  5. *
  6. */
  7. public class MyEvent extends ApplicationContextEvent {
  8. private static final long serialVersionUID = 1L;
  9. public MyEvent(ApplicationContext source) {
  10. super(source);
  11. System.out.println("myEvent 构造方法被执行了...");
  12. }
  13. public void out(String name){
  14. System.out.println("myEvent .... out方法执行了"+name);
  15. }
  16. }

2.事件监听类

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

  1. /**
  2. * 监听器
  3. * 观察者
  4. * @author 波波烤鸭
  5. * @email dengpbs@163.com
  6. *
  7. */
  8. public class MyListenerA implements ApplicationListener<MyEvent>{
  9. @Override
  10. public void onApplicationEvent(MyEvent event) {
  11. System.out.println("MyListenerA 监听器触发了...");
  12. // 执行事件中的特定方法
  13. event.out("AAAAA");
  14. }
  15. }
  1. /**
  2. * 监听器
  3. * 观察者
  4. * @author 波波烤鸭
  5. * @email dengpbs@163.com
  6. *
  7. */
  8. public class MyListenerB implements ApplicationListener<MyEvent>{
  9. @Override
  10. public void onApplicationEvent(MyEvent event) {
  11. System.out.println("MyListenerB 监听器触发了...");
  12. // 执行事件中的特定方法
  13. event.out("BBBBB");
  14. }
  15. }

3.事件发布者

  1. /**
  2. * 事件发布类
  3. * 实现ApplicationContextAware接口用来感知ApplicationContext对象
  4. * @author 波波烤鸭
  5. * @email dengpbs@163.com
  6. *
  7. */
  8. public class MyPublisher implements ApplicationContextAware{
  9. public ApplicationContext ac;
  10. @Override
  11. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  12. // TODO Auto-generated method stub
  13. this.ac = applicationContext;
  14. }
  15. /**
  16. * 发布事件
  17. * 监听该事件的监听者都可以获取消息
  18. * @param event
  19. */
  20. public void publisherEvent(ApplicationEvent event){
  21. System.out.println("---发布事件---"+event);
  22. ac.publishEvent(event);
  23. }
  24. }

4.配置文件中注册

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  7. <context:annotation-config/>
  8. <bean class="com.dpb.pojo.User" id="user" >
  9. <property name="name" value="波波烤鸭"></property>
  10. </bean>
  11. <!-- 注册事件类 -->
  12. <bean class="com.dpb.event.MyEvent"></bean>
  13. <!-- 注册监听器 -->
  14. <bean class="com.dpb.listener.MyListenerA"></bean>
  15. <bean class="com.dpb.listener.MyListenerB"></bean>
  16. <!-- 注册发布者类 -->
  17. <bean class="com.dpb.publisher.MyPublisher"></bean>
  18. </beans>

5.测试

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

输出结果

  1. myEvent 构造方法被执行了...
  2. ---发布事件---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]
  3. MyListenerA 监听器触发了...
  4. myEvent .... out方法执行了AAAAA
  5. MyListenerB 监听器触发了...
  6. myEvent .... out方法执行了BBBBB

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

二、Spring中事件监听分析

1. Spring中事件监听的结构

2. 核心角色介绍

2.1 ApplicationEvent

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

  1. public abstract class ApplicationEvent extends EventObject {
  2. /** use serialVersionUID from Spring 1.2 for interoperability */
  3. private static final long serialVersionUID = 7099057708183571937L;
  4. /** System time when the event happened */
  5. private final long timestamp;
  6. /**
  7. * Create a new ApplicationEvent.
  8. * @param source the object on which the event initially occurred (never {@code null})
  9. */
  10. public ApplicationEvent(Object source) {
  11. super(source);
  12. this.timestamp = System.currentTimeMillis();
  13. }
  14. /**
  15. * Return the system time in milliseconds when the event happened.
  16. */
  17. public final long getTimestamp() {
  18. return this.timestamp;
  19. }
  20. }

实现类:

2.2 ApplicationListener

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

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

实现类

2.3 ApplicationContext

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

  1. public interface ApplicationEventPublisher {
  2. /**
  3. * Notify all <strong>matching</strong> listeners registered with this
  4. * application of an application event. Events may be framework events
  5. * (such as RequestHandledEvent) or application-specific events.
  6. * @param event the event to publish
  7. * @see org.springframework.web.context.support.RequestHandledEvent
  8. */
  9. void publishEvent(ApplicationEvent event);
  10. /**
  11. * Notify all <strong>matching</strong> listeners registered with this
  12. * application of an event.
  13. * <p>If the specified {@code event} is not an {@link ApplicationEvent},
  14. * it is wrapped in a {@link PayloadApplicationEvent}.
  15. * @param event the event to publish
  16. * @since 4.2
  17. * @see PayloadApplicationEvent
  18. */
  19. void publishEvent(Object event);
  20. }



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

  1. protected void publishEvent(Object event, ResolvableType eventType) {
  2. Assert.notNull(event, "Event must not be null");
  3. if (logger.isTraceEnabled()) {
  4. logger.trace("Publishing event in " + getDisplayName() + ": " + event);
  5. }
  6. // Decorate event as an ApplicationEvent if necessary
  7. ApplicationEvent applicationEvent;
  8. if (event instanceof ApplicationEvent) {
  9. applicationEvent = (ApplicationEvent) event;
  10. }
  11. else {
  12. applicationEvent = new PayloadApplicationEvent<Object>(this, event);
  13. if (eventType == null) {
  14. eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
  15. }
  16. }
  17. // Multicast right now if possible - or lazily once the multicaster is initialized
  18. if (this.earlyApplicationEvents != null) {
  19. this.earlyApplicationEvents.add(applicationEvent);
  20. }
  21. else {
  22. getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
  23. }
  24. // Publish event via parent context as well...
  25. if (this.parent != null) {
  26. if (this.parent instanceof AbstractApplicationContext) {
  27. ((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
  28. }
  29. else {
  30. this.parent.publishEvent(event);
  31. }
  32. }
  33. }

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

2.4 ApplicationEventMulticaster

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



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

  1. @Override
  2. public void addApplicationListener(ApplicationListener<?> listener) {
  3. Assert.notNull(listener, "ApplicationListener must not be null");
  4. if (this.applicationEventMulticaster != null) {
  5. this.applicationEventMulticaster.addApplicationListener(listener);
  6. }
  7. else {
  8. this.applicationListeners.add(listener);
  9. }
  10. }

三、总结

  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. Oracle截取JSON字符串内容

    CREATE OR REPLACE FUNCTION PLATFROM.parsejsonstr(p_jsonstr varchar2,startkey varchar2,endkey varchar ...

  2. vue中实现图片全屏缩放预览,支持移动端

    # 安装 npm install vue-photo-preview --save # 引入 import preview from 'vue-photo-preview' import 'vue-p ...

  3. Scanner,Random,匿名对象-------------------java基础学习第七天

    1.API 2.Scanner 功能:通过键盘输入数据到程序中. 引用类型的一般使用步骤: 导包 Import 包路径.类名称 只有java.lang 包写的类不需要导包,其他都需要 2.创建 类名称 ...

  4. 课堂作业Complex类的实现

    #include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex ...

  5. 俄罗斯方块(三):"流动"的方块

    问题的提出: 俄罗斯方块允许90度的坡,是不是有点不够科学#(滑稽) 想办法加一种会“滑坡”的方块 本文两大部分: 详细的描绘是怎样的“流动” 写代码,并整合进游戏 本文基于我写的 俄罗斯方块(一): ...

  6. VIM批量缩进

    方法一 1.按 ctrl + shif + ;  进入底行模式 2.将所要批量缩进的行号写上,按照格式:“行号1,行号2>”输入命令,如要将4至11行批量缩进一个tab值,则命令为“4,11&g ...

  7. c# 钩子类

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  8. Data Center手册(4):设计

    基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功 ...

  9. [BlueZ] 1、Download install and use the BlueZ and hcitool on PI 3B+

    星期日, 02. 九月 2018 11:58下午 - beautifulzzzz 1. Introduction Bluez is the default Bluetooth protocol sta ...

  10. 利用Python实现对Web服务器的目录探测

    今天是一篇提升技能的干货分享,操作性较强,适用于中级水平的小伙伴,文章阅读用时约3分钟. PART 1/Python Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python ...