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

SpringBoot中ApplicationEvent的使用步驟詳解

 更新時(shí)間:2024年04月13日 10:57:57   作者:南瓜小米粥、  
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式,本文給大家介紹SpringBoot中ApplicationEvent的使用步驟詳解,感興趣的朋友跟隨小編一起看看吧

介紹

 ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式。相對于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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

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

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

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

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

    Java使用多線程批次查詢大量數(shù)據(jù)(Callable返回?cái)?shù)據(jù))方式

    今天給大家分享Java使用多線程批次查詢大量數(shù)據(jù)(Callable返回?cái)?shù)據(jù))方式,多線程有好幾種方式,今天說的方式比較好,實(shí)現(xiàn)Callable<> 這種方式能返回查詢的數(shù)據(jù),加上Future異步獲取方式,查詢效率大大加快,感興趣的朋友一起看看吧
    2023-11-11
  • SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)

    SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)

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

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

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

    Java的Shiro框架認(rèn)證流程詳解

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

    Java中的Kafka消費(fèi)者詳解

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

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

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

最新評論

周口市| 洛隆县| 桃源县| 阿瓦提县| 迭部县| 礼泉县| 桃园市| 东乡族自治县| 宜昌市| 名山县| 大理市| 赤峰市| 监利县| 高青县| 文昌市| 科尔| 南通市| 永川市| 辛集市| 吴忠市| 拜城县| 宁安市| 秦皇岛市| 类乌齐县| 稻城县| 彭泽县| 罗田县| 东海县| 化隆| 蕲春县| 四会市| 六盘水市| 德化县| 苏尼特右旗| 大化| 黎平县| 建始县| 汤阴县| 当阳市| 阿克陶县| 余江县|