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)聽消息
此步驟相當于MQ的消費者,實現(xiàn)ApplicatonListener類,通過泛型來設置消息類型。
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());
}
}到此這篇關于SpringBoot中ApplicationEvent的用法的文章就介紹到這了,更多相關SpringBoot ApplicationEvent用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
spring注入在有常量的情況下使用@AllArgsConstructor操作
這篇文章主要介紹了spring注入在有常量的情況下使用@AllArgsConstructor操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java使用多線程批次查詢大量數(shù)據(jù)(Callable返回數(shù)據(jù))方式
今天給大家分享Java使用多線程批次查詢大量數(shù)據(jù)(Callable返回數(shù)據(jù))方式,多線程有好幾種方式,今天說的方式比較好,實現(xiàn)Callable<> 這種方式能返回查詢的數(shù)據(jù),加上Future異步獲取方式,查詢效率大大加快,感興趣的朋友一起看看吧2023-11-11
SpringBoot項目如何把接口參數(shù)中的空白值替換為null值(推薦)
這篇文章主要介紹了SpringBoot項目如何把接口參數(shù)中的空白值替換為null值(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式
這篇文章主要介紹了Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2021-07-07
springboot HandlerIntercepter攔截器修改request body數(shù)據(jù)的操作
這篇文章主要介紹了springboot HandlerIntercepter攔截器修改request body數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。2021-06-06

