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

SpringBoot事件發(fā)布和監(jiān)聽詳解

 更新時間:2021年11月12日 11:43:27   作者:ForeverKobe  
今天去官網(wǎng)查看spring boot資料時,在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

概述

ApplicationEvent以及Listener是Spring為我們提供的一個事件監(jiān)聽、訂閱的實現(xiàn),內(nèi)部實現(xiàn)原理是觀察者設(shè)計模式,設(shè)計初衷也是為了系統(tǒng)業(yè)務(wù)邏輯之間的解耦,提高可擴展性以及可維護性。事件發(fā)布者并不需要考慮誰去監(jiān)聽,監(jiān)聽具體的實現(xiàn)內(nèi)容是什么,發(fā)布者的工作只是為了發(fā)布事件而已。事件監(jiān)聽的作用與消息隊列有一點類似。

事件監(jiān)聽的結(jié)構(gòu)

主要有三個部分組成:

  1. 發(fā)布者Publisher
  2. 事件Event
  3. 監(jiān)聽者Listener

Publisher,Event和Listener的關(guān)系

事件

我們自定義事件MyTestEvent繼承了ApplicationEvent,繼承后必須重載構(gòu)造函數(shù),構(gòu)造函數(shù)的參數(shù)可以任意指定,其中source參數(shù)指的是發(fā)生事件的對象,一般我們在發(fā)布事件時使用的是this關(guān)鍵字代替本類對象,而user參數(shù)是我們自定義的注冊用戶對象,該對象可以在監(jiān)聽內(nèi)被獲取。

@Getter
public class MyTestEvent extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    private User user;

    public MyTestEvent(Object source, User user) {
        super(source);
        this.user = user;
    }
}

發(fā)布者

事件發(fā)布是由ApplicationContext對象管控的,我們發(fā)布事件前需要注入ApplicationContext對象調(diào)用publishEvent方法完成事件發(fā)布。

ApplicationEventPublisher applicationEventPublisher 雖然聲明的是ApplicationEventPublisher,但是實際注入的是applicationContext

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("testEvent")
    public void test() {
        applicationEventPublisher.publishEvent(new MyTestEvent("dzf-casfd-111", new User("dzf-625096527-111", "xiaoming", 19)));
        applicationEventPublisher.publishEvent(new MyTestEvent("dzf-49687489-111", new User("dzf-625096527-111", "xiaowang", 20)));
    }


}

監(jiān)聽者

面向接口編程,實現(xiàn)ApplicationListener接口

@Component
public class MyTestListener implements ApplicationListener<MyTestEvent> {

    @Override
    public void onApplicationEvent(MyTestEvent myTestEvent) {
        System.out.println("MyTestListener : " + myTestEvent.getUser());
    }
}

使用@EventListener注解配置

@Component
public class MyTestListener2{

    @EventListener(MyTestEvent.class)
    public void onApplicationEvent(MyTestEvent myTestEvent) {
        System.out.println("MyTestListener2:" + myTestEvent.getUser());
    }
}

總結(jié)

到此這篇關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的文章就介紹到這了,更多相關(guān)SpringBoot事件發(fā)布和監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java設(shè)計模式學習之簡單工廠模式

    java設(shè)計模式學習之簡單工廠模式

    這篇文章主要為大家詳細介紹了java設(shè)計模式學習之簡單工廠模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • MyBatis根據(jù)條件批量修改字段的方式

    MyBatis根據(jù)條件批量修改字段的方式

    這篇文章主要介紹了MyBatis根據(jù)條件批量修改字段的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 最新評論

    韶山市| 庆城县| 鄂尔多斯市| 绥宁县| 金坛市| 福海县| 富民县| 芦溪县| 阿拉善盟| 望谟县| 大安市| 中山市| 满城县| 台前县| 左贡县| 昌平区| 文安县| 湖州市| 布拖县| 临汾市| 鞍山市| 砀山县| 黄浦区| 平阳县| 新丰县| 岗巴县| 太原市| 称多县| 贡嘎县| 南宁市| 安义县| 郧西县| 汝阳县| 海宁市| 陵水| 大关县| 堆龙德庆县| 古丈县| 治县。| 临沭县| 南溪县|