Springboot中自定义监听器
一、监听器模式图

二、监听器三要素
- 广播器:用来发布事件
- 事件:需要被传播的消息
- 监听器:一个对象对一个事件的发生做出反应,这个对象就是事件监听器
三、监听器的实现方式
1、实现自定义事件
自定义事件需要继承ApplicationEvent类,并添加一个构造函数,用于接收事件源对象。
该事件中添加了一个SysUser对象,用于传递用户信息。
package com.ruoyi.web.listener;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.ApplicationEvent;
/**
* @Description: 自定义事件
* @Author: baiwen
* @createTime: 2024年06月19日 13:10:07
*/
public class MyEvent extends ApplicationEvent {
private SysUser sysUser;
public MyEvent(Object source, SysUser sysUser) {
super(source);
this.sysUser = sysUser;
}
public SysUser getSysUser() {
return sysUser;
}
}
2、实现自定义监听器
自定义监听器需要实现ApplicationListener接口,并重写 onApplicationEvent方法。
接口中的泛型参数为自定义事件类型,表示监听该类型的事件。
可以从该事件中获取用户信息,并进行相应的处理。
package com.ruoyi.web.listener;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @Description: 自定义监听器
* @Author: baiwen
* @createTime: 2024年06月19日 13:12:39
*/
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
SysUser sysUser = event.getSysUser();
System.out.println("监听到了事件,用户名:" + sysUser.getUserName());
}
}
3、发布自定义事件
在需要发布事件的地方,使用ApplicationEventPublisher的publishEvent方法来发布事件。
这里使用Test类来模拟事件发布,实际应用中可以根据具体需求来选择合适的发布场景。
package com.ruoyi.test;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.listener.MyEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @Description:
* @Author: baiwen
* @createTime: 2024年06月19日 13:16:33
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyEventPushTest {
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@Test
public void testpublishEvent() throws InterruptedException
{
SysUser sysUser = new SysUser();
sysUser.setUserName("zhangsan");
System.out.println("发布MyEvent事件。。。");
applicationEventPublisher.publishEvent(new MyEvent(this, sysUser));
}
}
4、测试
运行MyEventPushTest类中的testpublishEvent方法,控制台会输出以下内容:
发布MyEvent事件。。。
监听到了事件,用户名:zhangsan
5、其他实现方案
主要是监听器的注册方式不同,目的只有一个,把监听器加入到spring容器中。
方式一,就是上面的MyEventListener类是通过@Component注解将该类注册为Spring的Bean,从而实现监听器的功能。
方式二,可以通过在启动类中添加监听器的方式,使监听器生效。
package com.ruoyi;
import com.ruoyi.web.listener.MyEventListener;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
/**
* 启动程序
*
* @author baiwen
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
public static void main(String[] args)
{
new SpringApplicationBuilder(RuoYiApplication.class).listeners(new MyEventListener()).run(args);
}
}
方式三,可以通过配置spring.factories,使监听器生效。
在resource文件夹下创建META-INF/spring.factories文件。

配置内容如下:
# 监听器
org.springframework.context.ApplicationListener=com.ruoyi.web.listener.MyEventListener
除此之外,还有第四种方式,通过@EventListener注解实现监听器的功能。
通过@EventListener注解的condition属性来指定监听的事件类型。
package com.ruoyi.web.listener;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* @Description: 自定义监听器2
* @Author: baiwen
* @createTime: 2024年06月19日 14:07:57
*/
@Component
public class MyEventListener2 {
@EventListener(MyEvent.class)
public void listenerApplicationStarted(MyEvent event) {
SysUser sysUser = event.getSysUser();
System.out.println("注解方式监听到了事件,用户名:" + sysUser.getUserName());
}
}
发布事件后,可以看到能正常监听到事件。
发布MyEvent事件。。。
注解方式监听到了事件,用户名:zhangsan
总结
以上,就是SpringBoot中实现监听器的四种方式。
至于监听器的实现原理,后续再补充。
Springboot中自定义监听器的更多相关文章
- 详解Springboot中自定义SpringMVC配置
详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...
- springboot中自定义根路径的配置
Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application ...
- SpringBoot中自定义properties文件配置参数并带有输入提示
1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...
- SpringBoot中自定义错误页面
错误页面定制(在有模板引擎的情况下): 有模板的支持下: 在templates文件夹下 建立 error文件夹 在error文件夹下 404.html 500.html 4xx.html (名字就叫4 ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
- springboot中使用自定义两级缓存
工作中用到了springboot的缓存,使用起来挺方便的,直接引入redis或者ehcache这些缓存依赖包和相关缓存的starter依赖包,然后在启动类中加入@EnableCaching注解,然后在 ...
- springboot中使用拦截器、监听器、过滤器
拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor) 我们需要对一个类实现HandlerInterceptor接 ...
- springboot(整合多数据源demo,aop,定时任务,异步方法调用,以及获取properties中自定义的变量值)
有这么一个需求 每个部门,需要操作的数据库不同,A部门要将数据放test数据库,B 部门数据 要放在test1数据库 同一个项目 需要整合 多个数据源 上传个demo 方便自己以后回看!!!!!!!! ...
- Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法
1.Button自己在xml文件中绑定监听器 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
随机推荐
- Selenium使用总结:加载Flash、禁用JS、滚动页面至元素、缩放页面
前言 前几周做了个使用Selenium的项目,踩了好多好多好多的Selenium的坑,越来越感觉他作为一个第三方库,对于Chrome的操作实在是有局限.另外,推荐大家一个Selenium之外的操作浏览 ...
- ruby http请求组件
github地址 https://github.com/rest-client/rest-client gemfile里添加 gem 'rest-client', '~> 2.0' 执行 bun ...
- Go类型断言demo
Go类型断言demo package main import ( "bytes" "encoding/json" "fmt" "i ...
- 用 C 语言开发一门编程语言 — 异常处理
目录 文章目录 目录 前文列表 异常捕获 定义 Lisp Value 函数 前文列表 <用 C 语言开发一门编程语言 - 交互式解析器l> <用 C 语言开发一门编程语言 - 跨平台 ...
- AIRIOT答疑第6期|如何使用二次开发引擎?
灵活扩展,满足客户定制化需求 AIRIOT物联网低代码平台提供丰富的前端.后台服务二次开发接口,具备灵活的组件服务部署与管理能力,对任何功能模块进行二次开发,满足客户各类二次开发需求.支持多种 ...
- Python:Python对象模型与序列迭代陷阱
1. Python对象模型与浅拷贝/深拷贝 1.1 Python对象模型和引用 在我们讲解Python的序列修改陷阱之前,先巩固一下Python的对象模型和浅拷贝/深拷贝的知识. 众所周知,Pytho ...
- VisualStduio如何自定义代码片段
什么是代码片段 代码片段又叫CodeSnippet,可以用来快捷补全代码.其实我们经常使用这个功能. 比如for循环,输入for会弹出这个窗口 我们选择第二个for,再按两次Tab键,编辑器就会自动生 ...
- 利用FileReader进行二进制文件传输
一.读取本地二进制文件,上传(数据库文件为例) 二进制文件读取的时候应当直接读取成字节数组,以免在调试时造成误解.比如数据库文件里面的有些字段是utf8编码,因此,采用utf-8编码读出来也能看到一些 ...
- NFS共享文件
NFS共享文件 服务端 安装NFS [root@localhost www] yum -y install nfs-utils rpcbind 创建需要共享的文件夹share [root@localh ...
- FMEA学习之PFMEA
一.基础介绍 FMEA 是 Faliure Mode Effect Analysis 简称,翻译过来叫做失效模式分析,按我的理解,用白话说出来就是:对导致不符合生产质量不符合客户要求的问题会产生多么严 ...