Springboot中如何自定義監(jiān)聽器
一、監(jiān)聽器模式圖

二、監(jiān)聽器三要素廣播器:
- 廣播器:用來發(fā)布事件
- 事件:需要被傳播的消息
- 監(jiān)聽器:一個(gè)對象對一個(gè)事件的發(fā)生做出反應(yīng),這個(gè)對象就是事件監(jiān)聽器
三、監(jiān)聽器的實(shí)現(xiàn)方式
1、實(shí)現(xiàn)自定義事件
自定義事件需要繼承ApplicationEvent類,并添加一個(gè)構(gòu)造函數(shù),用于接收事件源對象。
該事件中添加了一個(gè)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、實(shí)現(xiàn)自定義監(jiān)聽器
自定義監(jiān)聽器需要實(shí)現(xiàn)ApplicationListener接口,并重寫 onApplicationEvent方法。
接口中的泛型參數(shù)為自定義事件類型,表示監(jiān)聽該類型的事件。
可以從該事件中獲取用戶信息,并進(jìn)行相應(yīng)的處理。
package com.ruoyi.web.listener;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @Description: 自定義監(jiān)聽器
* @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("監(jiān)聽到了事件,用戶名:" + sysUser.getUserName());
}
}3、發(fā)布自定義事件
在需要發(fā)布事件的地方,使用ApplicationEventPublisher的publishEvent方法來發(fā)布事件。
這里使用Test類來模擬事件發(fā)布,實(shí)際應(yīng)用中可以根據(jù)具體需求來選擇合適的發(fā)布場景。
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("發(fā)布MyEvent事件。。。");
applicationEventPublisher.publishEvent(new MyEvent(this, sysUser));
}
}4、測試
運(yùn)行MyEventPushTest類中的testpublishEvent方法,控制臺會輸出以下內(nèi)容:
發(fā)布MyEvent事件。。。
監(jiān)聽到了事件,用戶名:zhangsan
5、其他實(shí)現(xiàn)方案
主要是監(jiān)聽器的注冊方式不同,目的只有一個(gè),把監(jiān)聽器加入到spring容器中。
方式一,就是上面的MyEventListener類是通過@Component注解將該類注冊為Spring的Bean,從而實(shí)現(xiàn)監(jiān)聽器的功能。
方式二,可以通過在啟動類中添加監(jiān)聽器的方式,使監(jiān)聽器生效。
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,使監(jiān)聽器生效。
在resource文件夾下創(chuàng)建META-INF/spring.factories文件。

配置內(nèi)容如下:
# 監(jiān)聽器 org.springframework.context.ApplicationListener=com.ruoyi.web.listener.MyEventListener
除此之外,還有第四種方式,通過@EventListener注解實(shí)現(xiàn)監(jiān)聽器的功能。
通過@EventListener注解的condition屬性來指定監(jiān)聽的事件類型。
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: 自定義監(jiān)聽器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("注解方式監(jiān)聽到了事件,用戶名:" + sysUser.getUserName());
}
}發(fā)布事件后,可以看到能正常監(jiān)聽到事件。
發(fā)布MyEvent事件。。。 注解方式監(jiān)聽到了事件,用戶名:zhangsan
???????總結(jié)
以上,就是SpringBoot中實(shí)現(xiàn)監(jiān)聽器的四種方式。
至于監(jiān)聽器的實(shí)現(xiàn)原理,后續(xù)再補(bǔ)充。
到此這篇關(guān)于Springboot中自定義監(jiān)聽器的文章就介紹到這了,更多相關(guān)Springboot自定義監(jiān)聽器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中過濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證
本文主要介紹了SpringBoot中過濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
Spring Data JPA實(shí)現(xiàn)排序與分頁查詢超詳細(xì)流程講解
在介紹Spring Data JPA的時(shí)候,我們首先認(rèn)識下Hibernate。Hibernate是數(shù)據(jù)訪問解決技術(shù)的絕對霸主,使用O/R映射技術(shù)實(shí)現(xiàn)數(shù)據(jù)訪問,O/R映射即將領(lǐng)域模型類和數(shù)據(jù)庫的表進(jìn)行映射,通過程序操作對象而實(shí)現(xiàn)表數(shù)據(jù)操作的能力,讓數(shù)據(jù)訪問操作無須關(guān)注數(shù)據(jù)庫相關(guān)的技術(shù)2022-10-10
Java高級用法中的JNA類型映射注意細(xì)節(jié)及使用問題
本文介紹了在使用JNA方法映射中應(yīng)該注意的一些細(xì)節(jié)和具體的使用問題,對java??JNA類型映射注意細(xì)節(jié)感興趣的朋友一起看看吧2022-04-04
java實(shí)現(xiàn)文件上傳下載和圖片壓縮代碼示例
本文給大家介紹的是項(xiàng)目中經(jīng)常需要用到的一個(gè)常用的功能,使用java實(shí)現(xiàn)文件的上傳下載和圖片的壓縮功能,這里推薦給大家,有需要的小伙伴參考下。2015-03-03
去掉IntelliJ IDEA 中 mybatis 對應(yīng)的 xml 文件警告的教程圖解
本文通過圖文并茂的形式給大家介紹了去掉IntelliJ IDEA 中 mybatis 對應(yīng)的 xml 文件警告的教程,需要的朋友可以參考下2018-06-06
java 進(jìn)制轉(zhuǎn)換實(shí)例詳解
這篇文章主要介紹了java 進(jìn)制轉(zhuǎn)換實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
使用Apache Spark進(jìn)行Java數(shù)據(jù)分析的步驟詳解
今天我們將探討如何使用Apache Spark進(jìn)行Java數(shù)據(jù)分析,Apache Spark是一個(gè)強(qiáng)大的大數(shù)據(jù)處理引擎,它支持批處理和流處理,特別適合處理大規(guī)模數(shù)據(jù)集,在Java中使用Spark,我們可以利用其強(qiáng)大的數(shù)據(jù)處理能力來進(jìn)行各種數(shù)據(jù)分析任務(wù),需要的朋友可以參考下2024-07-07

