SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
對(duì)不起大家,昨天文章里的告別說早了,這個(gè)系列還不能就這么結(jié)束。
我們前面的文章中講解過RabbitMQ的用法,所謂MQ就是一種發(fā)布訂閱模式的消息模型。在Spring中其實(shí)本身也為我們提供了一種發(fā)布訂閱模式的事件處理方式,就是ApplicationEvent和 ApplicationListener,這是一種基于觀察者模式實(shí)現(xiàn)事件監(jiān)聽功能。也已幫助我們完成業(yè)務(wù)邏輯的解耦,提高程序的擴(kuò)展性和可維護(hù)性。
但是這里要注意ApplicationEvent和 MQ隊(duì)列雖然實(shí)現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個(gè)系統(tǒng)中,也就是要求必須是同一個(gè)Spring容器。
好了接下來我們就來演練一番。
在這個(gè)模型中,有兩個(gè)重要的類,一個(gè)是事件,一個(gè)是監(jiān)聽。事件要繼承ApplicationEvent類,監(jiān)聽要實(shí)現(xiàn)ApplicationListener接口。
一、開發(fā)ApplicationEvent事件
事件其實(shí)就是我們要發(fā)送的消息體,這個(gè)一般要根據(jù)我們的實(shí)際業(yè)務(wù)進(jìn)行封裝,需要什么類型的數(shù)據(jù),就是用什么類型,需要哪些字段就添加哪些字段。我們來給一個(gè)案例。
package com.lsqingfeng.springboot.applicationEvent; import lombok.Getter; import lombok.Setter; import org.springframework.context.ApplicationEvent; /** * @className: MyApplicationEvent * @description: 事件封裝 * @author: sh.Liu * @date: 2022-03-23 14:41 */ @Getter @Setter @ToString public class MyApplicationEvent extends ApplicationEvent { private Integer age; private String name; /** * 需要重寫構(gòu)造方法 * @param source * @param name * @param age */ public MyApplicationEvent(Object source, String name, Integer age) { super(source); this.name = name; this.age = age; } }
二、 開發(fā)監(jiān)聽器
監(jiān)聽器就相當(dāng)于我們的MQ的消費(fèi)者,當(dāng)有時(shí)間推送過來的時(shí)候,監(jiān)聽器的代碼就可以執(zhí)行。這里通過泛型來設(shè)置好我們的事件類型。
package com.lsqingfeng.springboot.applicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * @className: MyApplicationEventListener * @description:事件監(jiān)聽器 * @author: sh.Liu * @date: 2022-03-23 14:50 */ @Component public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> { @Override public void onApplicationEvent(MyApplicationEvent event) { System.out.println("收到消息:" + event); } }
三、推送事件
推送事件需要使用ApplicationEventPublisher。這個(gè)對(duì)象在Spring容器加載的時(shí)候就已經(jīng)在容器中了。所以我們可以直接注入使用,也可以使用ApplicationContext,因?yàn)锳pplicationContext本身就繼承了ApplicationEventPublisher。 我們通過一個(gè)Controller來驗(yàn)證一下。
package com.lsqingfeng.springboot.controller; import com.lsqingfeng.springboot.applicationEvent.MyApplicationEvent; import com.lsqingfeng.springboot.base.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @className: ApplicationEventController * @description: * @author: sh.Liu * @date: 2022-03-23 15:21 */ @RestController @RequestMapping("event") public class ApplicationEventController { @Autowired private ApplicationContext applicationContext; @RequestMapping("/push") public Result pushEvent(){ MyApplicationEvent myApplicationEvent = new MyApplicationEvent(this,"zhangsan", 10); applicationContext.publishEvent(myApplicationEvent); return Result.success(); } @RequestMapping("/push2") public Result pushEvent2(){ applicationContext.publishEvent("大家好"); return Result.success(); } }
我們定義兩個(gè)推送的方法。一個(gè)推送我們的MyApplicationEvent類型,還有一個(gè)方法推送一個(gè)字符串。
當(dāng)我們調(diào)用第一個(gè)方法的時(shí)候,控制臺(tái)可以打印出我們推送的數(shù)據(jù)信息。
調(diào)用推送字符串的時(shí)候,我們的監(jiān)聽器不會(huì)執(zhí)行,原因是我們的攔截器里已經(jīng)加了泛型MyApplicationEvent,也就是只會(huì)監(jiān)聽MyApplicationEvent類型的消息。其他類型的消息不會(huì)被監(jiān)聽到。
那如果我們把泛型去掉會(huì)有什么效果呢,我們來試試。
每次推送都會(huì)發(fā)送兩條(可能有什么內(nèi)部機(jī)制,不管了),但是兩個(gè)都打印了,說明如果不加泛型,不管誰推,這邊都能收到消息。
四、注解方式實(shí)現(xiàn)監(jiān)聽器
除了上面的通過實(shí)現(xiàn)接口的方式開發(fā)監(jiān)聽器,我們還可以通過注解的方式來實(shí)現(xiàn),具體代碼如下。
package com.lsqingfeng.springboot.applicationEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; /** * @className: MyApplicationEventListener2 * @description: 注解實(shí)現(xiàn)監(jiān)聽器 * @author: sh.Liu * @date: 2022-03-23 15:56 */ @Component public class MyApplicationEventListener2 { @EventListener public void onEvent(MyApplicationEvent event){ System.out.println("收到消息2:" + event); } }
這里加入了@EventListener 注解代表了這是一個(gè)監(jiān)聽器。方法名隨意,方法里的參數(shù)代表監(jiān)聽的事件類型。
再次調(diào)用push方法:
發(fā)現(xiàn)兩個(gè)監(jiān)聽器的數(shù)據(jù)都會(huì)打印。這一特點(diǎn)大家要注意一下。
好了,關(guān)于Spring中的ApplicationEvent和ApplicationListener我們就介紹這么多。
到此這篇關(guān)于SpringBoot中ApplicationEvent用法的文章就介紹到這了,更多相關(guān)Spring Boot ApplicationEvent用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring ApplicationListener監(jiān)聽器用法詳解
- Spring ApplicationListener的使用詳解
- Spring ApplicationListener源碼解析
- SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
- Spring事件監(jiān)聽器ApplicationListener源碼詳解
- SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
- Spring中ApplicationListener的使用解析
- spring中ApplicationListener的使用小結(jié)
相關(guān)文章
Springboot+hibernate實(shí)現(xiàn)簡(jiǎn)單的增刪改查示例
今天小編就為大家分享一篇Springboot+hibernate實(shí)現(xiàn)簡(jiǎn)單的增刪改查示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08SpringBoot+actuator和admin-UI實(shí)現(xiàn)監(jiān)控中心方式
這篇文章主要介紹了SpringBoot+actuator和admin-UI實(shí)現(xiàn)監(jiān)控中心方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05通過端口1433連接到主機(jī)127.0.0.1的 TCP/IP 連接失敗,錯(cuò)誤:“connect timed out”的解
這篇文章主要介紹了通過端口1433連接到主機(jī)127.0.0.1的 TCP/IP 連接失敗,錯(cuò)誤:“connect timed out”的解決方法,需要的朋友可以參考下2015-08-08Java中Executor和Executors的區(qū)別小結(jié)
在Java并發(fā)編程中,Executor是一個(gè)核心接口,提供了任務(wù)執(zhí)行的抽象方法,而Executors是一個(gè)工具類,提供了創(chuàng)建各種線程池的工廠方法,Executor關(guān)注任務(wù)的執(zhí)行,而Executors關(guān)注如何創(chuàng)建適合的執(zhí)行器,感興趣的可以了解一下2024-10-10SpringBoot如何在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源
這篇文章主要介紹了SpringBoot如何在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)
基礎(chǔ)扎不扎實(shí)只有在實(shí)戰(zhàn)中才能顯現(xiàn),本篇文章手把手帶你用Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01Java深入學(xué)習(xí)圖形用戶界面GUI之事件處理
這篇文章主要介紹了基于Java GUI 事件處理方式,一個(gè)圖形界面制作完成了,在程序開發(fā)中只是完成了起步的工作。要想讓一個(gè)組件都發(fā)揮自己的作用.就必須對(duì)所有的組件進(jìn)行事件處理2022-05-05