SpringBoot中ApplicationEvent的使用步驟詳解
介紹
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式。相對于MQ,其局限在于只能在同一個Spring容器中使用。
使用步驟
封裝消息
將要發(fā)送的內(nèi)容,封裝成一個bean,這個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;
}
}推送消息
推送消息時,注入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的消費者,實現(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());
}
}通過注解實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)教程之static五大應(yīng)用場景
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之static五大應(yīng)用場景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Elasticsearch查詢Range Query語法示例
這篇文章主要為大家介紹了Elasticsearch查詢Range Query語法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
詳解如何使用Java8?Steam流對Map進(jìn)行排序
這篇文章主要給大家詳細(xì)介紹了如何使用Java8?Steam流對Map進(jìn)行排序,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
Spring boot如何基于攔截器實現(xiàn)訪問權(quán)限限制
這篇文章主要介紹了Spring boot如何基于攔截器實現(xiàn)訪問權(quán)限限制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
springboot實現(xiàn)指定mybatis中mapper文件掃描路徑
這篇文章主要介紹了springboot實現(xiàn)指定mybatis中mapper文件掃描路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
使用@Order控制配置類/AOP/方法/字段的加載順序詳解
這篇文章主要介紹了使用@Order控制配置類/AOP/方法/字段的加載順序詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

