SpringBoot事件發(fā)布和監(jiān)聽詳解
概述
ApplicationEvent以及Listener是Spring為我們提供的一個(gè)事件監(jiān)聽、訂閱的實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)原理是觀察者設(shè)計(jì)模式,設(shè)計(jì)初衷也是為了系統(tǒng)業(yè)務(wù)邏輯之間的解耦,提高可擴(kuò)展性以及可維護(hù)性。事件發(fā)布者并不需要考慮誰去監(jiān)聽,監(jiān)聽具體的實(shí)現(xiàn)內(nèi)容是什么,發(fā)布者的工作只是為了發(fā)布事件而已。事件監(jiān)聽的作用與消息隊(duì)列有一點(diǎn)類似。
事件監(jiān)聽的結(jié)構(gòu)
主要有三個(gè)部分組成:
- 發(fā)布者Publisher
- 事件Event
- 監(jiān)聽者Listener
Publisher,Event和Listener的關(guān)系
事件
我們自定義事件MyTestEvent繼承了ApplicationEvent,繼承后必須重載構(gòu)造函數(shù),構(gòu)造函數(shù)的參數(shù)可以任意指定,其中source參數(shù)指的是發(fā)生事件的對(duì)象,一般我們?cè)诎l(fā)布事件時(shí)使用的是this關(guān)鍵字代替本類對(duì)象,而user參數(shù)是我們自定義的注冊(cè)用戶對(duì)象,該對(duì)象可以在監(jiān)聽內(nèi)被獲取。
@Getter public class MyTestEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; private User user; public MyTestEvent(Object source, User user) { super(source); this.user = user; } }
發(fā)布者
事件發(fā)布是由ApplicationContext對(duì)象管控的,我們發(fā)布事件前需要注入ApplicationContext對(duì)象調(diào)用publishEvent方法完成事件發(fā)布。
ApplicationEventPublisher applicationEventPublisher 雖然聲明的是ApplicationEventPublisher,但是實(shí)際注入的是applicationContext
@RestController @RequestMapping("/test") public class TestController { @Autowired ApplicationContext applicationContext; @Autowired ApplicationEventPublisher applicationEventPublisher; @GetMapping("testEvent") public void test() { applicationEventPublisher.publishEvent(new MyTestEvent("dzf-casfd-111", new User("dzf-625096527-111", "xiaoming", 19))); applicationEventPublisher.publishEvent(new MyTestEvent("dzf-49687489-111", new User("dzf-625096527-111", "xiaowang", 20))); } }
監(jiān)聽者
面向接口編程,實(shí)現(xiàn)ApplicationListener接口
@Component public class MyTestListener implements ApplicationListener<MyTestEvent> { @Override public void onApplicationEvent(MyTestEvent myTestEvent) { System.out.println("MyTestListener : " + myTestEvent.getUser()); } }
使用@EventListener注解配置
@Component public class MyTestListener2{ @EventListener(MyTestEvent.class) public void onApplicationEvent(MyTestEvent myTestEvent) { System.out.println("MyTestListener2:" + myTestEvent.getUser()); } }
總結(jié)
到此這篇關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的文章就介紹到這了,更多相關(guān)SpringBoot事件發(fā)布和監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解SpringBoot實(shí)現(xiàn)ApplicationEvent事件的監(jiān)聽與發(fā)布
- spring event 事件異步處理方式(發(fā)布,監(jiān)聽,異步處理)
- SpringBoot事件發(fā)布與監(jiān)聽超詳細(xì)講解
- Spring事件發(fā)布監(jiān)聽,順序監(jiān)聽,異步監(jiān)聽方式
- 詳解Spring事件發(fā)布與監(jiān)聽機(jī)制
- 解析Spring事件發(fā)布與監(jiān)聽機(jī)制
- 詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事件
- Spring的事件發(fā)布與監(jiān)聽方式案例講解
相關(guān)文章
使用socket實(shí)現(xiàn)網(wǎng)絡(luò)聊天室和私聊功能
這篇文章主要介紹了使用socket實(shí)現(xiàn)網(wǎng)絡(luò)聊天室和私聊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Java使用connectTo方法提高代碼可續(xù)性詳解
這篇文章主要介紹了Java使用connectTo方法提高代碼可續(xù)性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
這篇文章主要介紹了關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04