最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中ApplicationEvent的使用步驟詳解

 更新時間:2024年04月13日 10:57:57   作者:南瓜小米粥、  
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式,本文給大家介紹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 Boot項目的內(nèi)嵌容器的理由

    選擇Spring Boot項目的內(nèi)嵌容器的理由

    Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。這篇文章主要介紹了選擇Spring Boot項目的內(nèi)嵌容器,需要的朋友可以參考下
    2017-11-11
  • spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)

    spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)

    這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Java返回分頁結(jié)果集的封裝代碼實例

    Java返回分頁結(jié)果集的封裝代碼實例

    這篇文章主要介紹了java返回分頁結(jié)果集的封裝代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • spring注入在有常量的情況下使用@AllArgsConstructor操作

    spring注入在有常量的情況下使用@AllArgsConstructor操作

    這篇文章主要介紹了spring注入在有常量的情況下使用@AllArgsConstructor操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java使用多線程批次查詢大量數(shù)據(jù)(Callable返回數(shù)據(jù))方式

    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值(推薦)

    這篇文章主要介紹了SpringBoot項目如何把接口參數(shù)中的空白值替換為null值(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式

    Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式

    這篇文章主要介紹了Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下
    2021-07-07
  • Java的Shiro框架認證流程詳解

    Java的Shiro框架認證流程詳解

    這篇文章主要介紹了Java的Shiro框架認證流程詳解,Shiro 是一個功能強大和易于使用的安全框架,為開發(fā)人員提供一個直觀而全面的解決方案的認證,授權,加密,會話管理四大功能,需要的朋友可以參考下
    2024-01-01
  • Java中的Kafka消費者詳解

    Java中的Kafka消費者詳解

    這篇文章主要介紹了Java中的Kafka消費者詳解,Kafka是一個分布式流行消息系統(tǒng),通常用于大規(guī)模數(shù)據(jù)處理和實時數(shù)據(jù)流應用程序,它具有高吞吐量、可擴展性和容錯性的特點,需要的朋友可以參考下
    2023-09-09
  • springboot HandlerIntercepter攔截器修改request body數(shù)據(jù)的操作

    springboot HandlerIntercepter攔截器修改request body數(shù)據(jù)的操作

    這篇文章主要介紹了springboot HandlerIntercepter攔截器修改request body數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。
    2021-06-06

最新評論

高台县| 察雅县| 北碚区| 镶黄旗| 汉阴县| 上栗县| 十堰市| 通山县| 兰考县| 华蓥市| 宜兰县| 锦屏县| 宜君县| 时尚| 双辽市| 泰兴市| 通道| 饶河县| 馆陶县| 加查县| 武夷山市| 包头市| 荣成市| 马尔康县| 高密市| 新乡县| 赤水市| 棋牌| 大丰市| 革吉县| 上饶市| 习水县| 吉木萨尔县| 远安县| 府谷县| 昂仁县| 托克逊县| 桓台县| 陇南市| 曲阳县| 张掖市|