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

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

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

概述

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

事件監(jiān)聽的結構

主要有三個部分組成:

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

Publisher,Event和Listener的關系

事件

我們自定義事件MyTestEvent繼承了ApplicationEvent,繼承后必須重載構造函數(shù),構造函數(shù)的參數(shù)可以任意指定,其中source參數(shù)指的是發(fā)生事件的對象,一般我們在發(fā)布事件時使用的是this關鍵字代替本類對象,而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());
    }
}

總結

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

相關文章

  • java設計模式學習之簡單工廠模式

    java設計模式學習之簡單工廠模式

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

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

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

    凉城县| 方城县| 中牟县| 临汾市| 申扎县| 福泉市| 华容县| 东丽区| 汉阴县| 景宁| 广水市| 龙岩市| 荆门市| 农安县| 宜城市| 泗洪县| 紫阳县| 芦山县| 敦化市| 巴中市| 霍林郭勒市| 义乌市| 师宗县| 怀来县| 邵阳县| 阿勒泰市| 曲麻莱县| 霍州市| 团风县| 华阴市| 辰溪县| 青岛市| 许昌市| 黎城县| 赣榆县| 格尔木市| 宁南县| 金沙县| 泸西县| 阜宁县| 佛教|