Spring Event

Spring Event是Spring的事件通知机制,可以将相互耦合的代码解耦,从而方便功能的修改与添加。Spring Event是监听者模式的一个具体实现。

监听者模式包含了监听者Listener、事件Event、事件发布者EventPublish,过程就是EventPublish发布一个事件,被监听者捕获到,然后执行事件相应的方法。

Spring Event的相关API在spring-context包中。

入门案例

第一步:创建maven工程springevent_demo并配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> </parent> <groupId>cn.itcast</groupId> <artifactId>springevent_demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

第二步:创建OptLogDTO类,用于封装操作日志信息

package cn.itcast.dto; import lombok.Data; @Data public class OptLogDTO { private String requestIp; //操作IP private String type; //日志类型 LogType{OPT:操作类型;EX:异常类型} private String userName; //操作人 private String description; //操作描述 }

第三步:创建事件类SysLogEvent

package cn.itcast.event; import cn.itcast.dto.OptLogDTO; import org.springframework.context.ApplicationEvent; /** * 定义系统日志事件 */ public class SysLogEvent extends ApplicationEvent { public SysLogEvent(OptLogDTO optLogDTO) { super(optLogDTO); } }

第四步:创建监听器类SysLogListener

package cn.itcast.listener; import cn.itcast.dto.OptLogDTO; import cn.itcast.event.SysLogEvent; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; /** * 异步监听日志事件 */ @Component public class SysLogListener { @Async//异步处理 @EventListener(SysLogEvent.class) public void saveSysLog(SysLogEvent event) { OptLogDTO sysLog = (OptLogDTO) event.getSource(); long id = Thread.currentThread().getId(); System.out.println("监听到日志操作事件:" + sysLog + " 线程id:" + id); //将日志信息保存到数据库... } }

第五步:创建Controller,用于发布事件

package cn.itcast.controller; import cn.itcast.dto.OptLogDTO; import cn.itcast.event.SysLogEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @Autowired private ApplicationContext applicationContext; @GetMapping("/getUser") public String getUser(){ //构造操作日志信息 OptLogDTO logInfo = new OptLogDTO(); logInfo.setRequestIp("127.0.0.1"); logInfo.setUserName("admin"); logInfo.setType("OPT"); logInfo.setDescription("查询用户信息"); //构造事件对象 ApplicationEvent event = new SysLogEvent(logInfo); //发布事件 applicationContext.publishEvent(event); long id = Thread.currentThread().getId(); System.out.println("发布事件,线程id:" + id); return "OK"; } }

第六步:创建启动类

package cn.itcast; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync//启用异步处理 public class SpringEventApp { public static void main(String[] args) { SpringApplication.run(SpringEventApp.class,args); } }

启动项目并访问Controller可以发现监听器触发了。


__EOF__

本文作者飞飞很要强
本文链接https://www.cnblogs.com/LiPengFeiii/p/15646412.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   飞飞很要强  阅读(382)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示