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

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr