Spring之事件监听(观察者模型)

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


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

Java观察者模式(Observer)

文章目录

   Spring事件监听

       一、事件监听案例

           1.事件类

           2.事件监听类

           3.事件发布者

           4.配置文件中注册

           5.测试

       二、Spring中事件监听分析

           1. Spring中事件监听的结构

           2. 核心角色介绍

               2.1 ApplicationEvent

               2.2 ApplicationListener

               2.3 ApplicationContext

               2.4 ApplicationEventMulticaster

       三、总结

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中事件监听的结构

image.png

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;
  }
}

实现类:

image.png

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);
}

实现类

image.png

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);
}

image.png

具体发布消息的方法实现: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广播给所有的监听器.

image.png

具体的注册监听是在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);
  }
}

三、总结

   Spring中的事件监听使用的是观察者模式

   所有事件需要继承ApplicationEvent父类

   所有的监听器需要实现ApplicationListener接口

   事件发布需要通过ApplicationContext中的publisherEvent方法实现

   监听器的注册是ApplicationEventMulticaster提供的,但我们并不需要实现。


相关文章
|
XML Java 数据格式
聊聊Spring事件及其应用
在 JDK 中已经提供相应的自定义事件发布功能的基础类: - `java.util.EventObject`类 :自定义**事件**类型 - `java.util.EventListener`接口:事件的**监听器**
125 1
聊聊Spring事件及其应用
|
存储 安全 Java
事件的力量:探索Spring框架中的事件处理机制
事件的力量:探索Spring框架中的事件处理机制
159 0
|
人工智能 JSON 前端开发
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
|
29天前
|
缓存 安全 Java
Spring Security通用权限管理模型解析
Spring Security作为Spring生态的核心安全框架,结合RBAC与ACL权限模型,基于IoC与AOP构建灵活、可扩展的企业级权限控制体系,涵盖认证、授权流程及数据库设计、性能优化等实现策略。
120 0
|
9月前
|
XML 监控 前端开发
Spring Boot中的WebFlux编程模型
Spring WebFlux 是 Spring Framework 5 引入的响应式编程模型,基于 Reactor 框架,支持非阻塞异步编程,适用于高并发和 I/O 密集型应用。本文介绍 WebFlux 的原理、优势及在 Spring Boot 中的应用,包括添加依赖、编写响应式控制器和服务层实现。WebFlux 提供高性能、快速响应和资源节省等优点,适合现代 Web 应用开发。
1005 15
|
9月前
|
Java Spring
Java Spring Boot监听事件和处理事件
通过上述步骤,我们可以在Java Spring Boot应用中实现事件的发布和监听。事件驱动模型可以帮助我们实现组件间的松耦合,提升系统的可维护性和可扩展性。无论是处理业务逻辑还是系统事件,Spring Boot的事件机制都提供了强大的支持和灵活性。希望本文能为您的开发工作提供实用的指导和帮助。
341 15
|
NoSQL Java Redis
Spring Boot 监听 Redis Key 失效事件实现定时任务
Spring Boot 监听 Redis Key 失效事件实现定时任务
266 0
|
人工智能 移动开发 Java
Java智能之Spring AI:5分钟打造智能聊天模型的利器
尽管Python最近成为了编程语言的首选,但是Java在人工智能领域的地位同样不可撼动,得益于强大的Spring框架。随着人工智能技术的快速发展,我们正处于一个创新不断涌现的时代。从智能语音助手到复杂的自然语言处理系统,人工智能已经成为了现代生活和工作中不可或缺的一部分。在这样的背景下,Spring AI 项目迎来了发展的机遇。尽管该项目汲取了Python项目如LangChain和LlamaIndex的灵感,但Spring AI并不是简单的移植。该项目的初衷在于推进生成式人工智能应用程序的发展,使其不再局限于Python开发者。
478 2
|
Java Spring 供应链
Spring 框架事件发布与监听机制,如魔法风暴席卷软件世界,开启奇幻编程之旅!
【8月更文挑战第31天】《Spring框架中的事件发布与监听机制》介绍了Spring中如何利用事件发布与监听机制实现组件间的高效协作。这一机制像城市中的广播系统,事件发布者发送消息,监听器接收并响应。通过简单的示例代码,文章详细讲解了如何定义事件类、创建事件发布者与监听器,并确保组件间松散耦合,提升系统的可维护性和扩展性。掌握这一机制,如同拥有一把开启高效软件开发大门的钥匙。
105 0
|
存储 设计模式 Java
Spring Boot中的事件溯源模式
Spring Boot中的事件溯源模式