spring|spring event|Spring内置事件驱动编程模型

spring|spring event|Spring内置事件驱动编程模型

事件驱动编程模型

基于经典的观察者模式实现,当一个组件(发布者)发生了某件重要的事(如用户注册),它只需"广播"一个事件,而无需关心谁会处理。其他对此感兴趣的组件(监听器)可以"订阅"并响应这个事件,彼此互不直接依赖

核心角色

  • 事件(Event)

信息的载体,封装了需要传递的数据。在Spring 4.2之后,它可以是任何普通的Java对象(POJO),不再强制继承ApplicationEvent类

  • 事件发布者(Publisher)

负责发布事件的组件。通过注入 ApplicationEventPublisher 接口并调用其 publishEvent 方法来实现

  • 事件监听器(Listener)

负责监听并处理特定事件的组件。通过实现 ApplicationListener 接口或使用 @EventListener 注解来定义

源码

https://gitee.com/jysemel-spring/spring-boot/tree/master/action/action-event

源码流程

  • 发布

业务逻辑触发时通过 ApplicationEventPublisher 发布一个事件对象

  • 广播

ApplicationContext 作为"广播器",会将接收到的事件分发给所有注册的、对该事件类型感兴趣的监听器

  • 处理

监听器接收到事件后,执行其定义好的业务逻辑

demo 源码

  • 定义事件类型

    package com.jysemel.spring.boot.study.event;

    import lombok.Getter;
    import lombok.Setter;
    import org.springframework.context.ApplicationEvent;

    @Setter
    @Getter
    public class UserRegisteredEvent extends ApplicationEvent {

    复制代码
      private String userName;
      private String nickName;
    
      public UserRegisteredEvent(Object source, String userName, String nickName) {
          super(source);
          this.userName = userName;
          this.nickName = nickName;
      }

    }

事件发布

复制代码
package com.jysemel.spring.boot.study.service;

import com.jysemel.spring.boot.study.event.UserRegisteredEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;


@Service
public class StoreService {

    @Autowired
    private ApplicationEventPublisher publisher;

    public void register(UserRegisteredEvent registeredEvent) {

        // 执行核心注册逻辑...
        System.out.println("用户 " + registeredEvent.getNickName() + " 注册成功");

        // 发布事件
        publisher.publishEvent(registeredEvent);
    }
}

事件监听

复制代码
package com.jysemel.spring.boot.study.listener;


import com.jysemel.spring.boot.study.event.UserRegisteredEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 方式二:使用@EventListener注解(更推荐)
 */
@Component
public class CouponListener {



    @EventListener
    public void handleUserRegisteredEvent(UserRegisteredEvent event) {
        System.out.println("方式二:使用@EventListener注解 ");
        System.out.println("赠送新人优惠券给用户:" + event.getNickName());
    }
}

演示结果

http://127.0.0.1:8083/api/user/reg?userName=113.9526\&nickName=22.5431

复制代码
用户 22.5431 注册成功
发送欢迎邮件给用户:22.5431
方式二:使用@EventListener注解 
赠送新人优惠券给用户:22.5431
相关推荐
SeaDhdhdhdhdh2 小时前
MCP Server 搭建与使用指南
java·ai·agent·mcp
YH55269843 小时前
GPT‑5.6 Sol 原本支持 1M 上下文,Codex 现已放开此前限制,如何看待这次调整?
java·jvm·人工智能·gpt·算法·chatgpt
智购科技智能售货柜3 小时前
2026自动售货机整机可靠性测试:从高低温交变到EMC电磁兼容的认证工程实践~YH
运维·服务器·数据库·人工智能·物联网
ltl3 小时前
RocksDB 架构演进:相对 LevelDB 的 diff 地图
数据库
ltl3 小时前
存储读取性能优化:索引、Bloom Filter 与读放大
数据库
AI_小站3 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
许彰午4 小时前
03-三种开发模式
java·架构
涟漪海洋4 小时前
创建最新的JDK25镜像,非root环境启动
java
Rain的Java大神之路5 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
2602_959960925 小时前
谢飞机面试大厂:Spring Boot、JVM、Redis、Kafka、微服务与音视频搜索场景求生实录
java·jvm·spring boot·redis·面试题