SpringBoot中ApplicationEvent的使用步驟詳解
介紹
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式。相對(duì)于MQ,其局限在于只能在同一個(gè)Spring容器中使用。
使用步驟
封裝消息
將要發(fā)送的內(nèi)容,封裝成一個(gè)bean,這個(gè)bean需要繼承ApplicationEvent類。
package com.example.event; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.context.ApplicationEvent; /** * @Description: 封裝消息 * @author: zeal * @date: 2024年04月09日 10:22 */ @Setter @Getter @ToString public class UserLoginEvent extends ApplicationEvent { private Integer userId; private String token; public UserLoginEvent(Object source,Integer userId,String token) { super(source); this.userId=userId; this.token=token; } }
推送消息
推送消息時(shí),注入ApplicationEventPublisher或ApplicationContext均可,調(diào)用publishEvent()方法。
package com.example.event; 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; /** * @Description: 消息推送 * @author: zeal * @date: 2024年04月09日 10:26 */ @RestController @RequestMapping("event") public class UserLoginController { @Autowired private ApplicationContext applicationContext; @RequestMapping("/push") public void pushEvent(){ UserLoginEvent userLoginEvent=new UserLoginEvent(this,001,"zsaf"); applicationContext.publishEvent(userLoginEvent); } }
監(jiān)聽消息
此步驟相當(dāng)于MQ的消費(fèi)者,實(shí)現(xiàn)ApplicatonListener類,通過泛型來設(shè)置消息類型。
package com.example.event; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * @Description: 監(jiān)聽消息 * @author: zeal * @date: 2024年04月09日 10:25 */ @Component public class UserLoginEventListener implements ApplicationListener<UserLoginEvent> { @Override public void onApplicationEvent(UserLoginEvent event) { System.out.println("收到消息:"+event.toString()); } }
通過注解實(shí)現(xiàn)監(jiān)聽
package com.example.event; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; /** * @Description: 監(jiān)聽消息 * @author: zeal * @date: 2024年04月09日 10:25 */ @Component public class UserLoginEventListener{ @EventListener public void onApplicationEvent(UserLoginEvent event) { System.out.println("收到消息:"+event.toString()); } }
到此這篇關(guān)于SpringBoot中ApplicationEvent的用法的文章就介紹到這了,更多相關(guān)SpringBoot ApplicationEvent用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)教程之static五大應(yīng)用場(chǎng)景
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之static五大應(yīng)用場(chǎng)景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Elasticsearch查詢Range Query語(yǔ)法示例
這篇文章主要為大家介紹了Elasticsearch查詢Range Query語(yǔ)法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04詳解如何使用Java8?Steam流對(duì)Map進(jìn)行排序
這篇文章主要給大家詳細(xì)介紹了如何使用Java8?Steam流對(duì)Map進(jìn)行排序,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制
這篇文章主要介紹了Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10完美解決單例設(shè)計(jì)模式中懶漢式線程安全的問題
下面小編就為大家?guī)硪黄昝澜鉀Q單例設(shè)計(jì)模式中懶漢式線程安全的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12springboot實(shí)現(xiàn)指定mybatis中mapper文件掃描路徑
這篇文章主要介紹了springboot實(shí)現(xiàn)指定mybatis中mapper文件掃描路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06使用@Order控制配置類/AOP/方法/字段的加載順序詳解
這篇文章主要介紹了使用@Order控制配置類/AOP/方法/字段的加載順序詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02