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

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添