String boot 发布事件用法 使用ApplicationEvent和Listener来业务解耦

本文主要是介绍String boot 发布事件用法 使用ApplicationEvent和Listener来业务解耦,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、新建一个实体对象

package com.example.demo.model;import lombok.Data;@Data
public class UserModel {//用户名private String name;//密码private String password;}

二、新建一个Service以及控制器

UserService .java

package com.example.demo.service;import com.example.demo.model.UserModel;public interface UserService {/*** 用户注册* @param userModel*/void register(UserModel userModel);}
UserServiceImpl.java
package com.example.demo.service.impl;import com.example.demo.event.UserRegisterEvent;
import com.example.demo.model.UserModel;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl implements UserService {@AutowiredApplicationContext applicationContext;@Overridepublic void register(UserModel userModel) {userModel.setName("小明");userModel.setPassword("123456");applicationContext.publishEvent(new UserRegisterEvent(this,userModel));}
}
TestController.java
@Autowiredprivate UserService userService;@GetMapping(value = "/test2")public Object test2() {UserModel userModel = new UserModel();userService.register(userModel);return "success";}

三、新建一个UserRegisterEvent类用于发布事件

package com.example.demo.event;import com.example.demo.model.UserModel;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;@Getter
public class UserRegisterEvent extends ApplicationEvent {private UserModel userModel;public UserRegisterEvent(Object source, UserModel userModel) {super(source);this.userModel = userModel;}
}

四、使用@EventListener注解的方式来监听发布事件

package com.example.demo.listener;import com.example.demo.event.UserRegisterEvent;
import com.example.demo.model.UserModel;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;@Component
public class AnnotationRegisterListener {@EventListenerpublic void register(UserRegisterEvent userRegisterEvent){//获取注册用户对象UserModel user = userRegisterEvent.getUserModel();//../省略逻辑//输出注册用户信息System.out.println("@EventListener注册信息,用户名:"+user.getName()+",密码:"+user.getPassword());}
}

运行结果如下

@EventListener注册信息,用户名:小明,密码:123456


 

这篇关于String boot 发布事件用法 使用ApplicationEvent和Listener来业务解耦的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668012

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客