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
相关推荐
蜡笔小谢15 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
Flynt16 小时前
Postgres 19 实测:表膨胀清理,VACUUM FULL 锁到读写归零,REPACK 没停一秒
数据库·postgresql·性能优化
唐老板16 小时前
Spring AI 2.0 只支持 Boot 4
java
琳少_19 小时前
java程序设计笔记
java·开发语言·数据库·笔记
考虑考虑19 小时前
记一次@Async生产事故
spring boot·后端·spring
小荷才露尖尖角,早有蜻蜓立上头20 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix25021 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
SelectDB1 天前
Apache Doris 5.0 IVM 技术解析:物化视图增量刷新原理
数据库
CoderYanger1 天前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠1 天前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记