首頁  >  文章  >  Java  >  SpringBoot中ApplicationEvent和ApplicationListener怎麼使用

SpringBoot中ApplicationEvent和ApplicationListener怎麼使用

PHPz
PHPz轉載
2023-05-13 19:46:09954瀏覽
在這個模型中,有兩個重要的類,一個是事件,一個是監聽。事件要繼承ApplicationEvent類,監聽實作ApplicationListener介面。

一、開發ApplicationEvent事件

事件其實就是我們要傳送的訊息體,這個一般要根據我們的實際業務進行封裝,需要什麼類型的數據,就是用什麼類型,需要哪些欄位就新增哪些欄位。我們來給一個案例。

package com.lsqingfeng.springboot.applicationEvent;
 
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
 
/**
 * @className: MyApplicationEvent
 * @description: 事件封装
 * @author: sh.Liu
 * @date: 2022-03-23 14:41
 */
@Getter
@Setter
@ToString
public class MyApplicationEvent extends ApplicationEvent {
 
    private Integer age;
 
    private String name;
 
    /**
     * 需要重写构造方法
     * @param source
     * @param name
     * @param age
     */
    public MyApplicationEvent(Object source, String name, Integer age) {
        super(source);
        this.name = name;
        this.age = age;
    }
}

二、 開發監聽器

監聽器就相當於我們的MQ的消費者,當有時間推送過來的時候,監聽器的程式碼就可以執行。這裡透過泛型來設定好我們的事件類型。

package com.lsqingfeng.springboot.applicationEvent;
 
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
/**
 * @className: MyApplicationEventListener
 * @description:事件监听器
 * @author: sh.Liu
 * @date: 2022-03-23 14:50
 */
@Component
public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> {
 
    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("收到消息:" + event);
    }
}

三、推送事件

推送事件需要使用ApplicationEventPublisher。這個物件在Spring容器載入的時候就已經在容器中了。所以我們可以直接注入使用,也可以使用ApplicationContext,因為ApplicationContext本身就繼承了ApplicationEventPublisher。我們透過一個Controller來驗證一下。

package com.lsqingfeng.springboot.controller;
 
import com.lsqingfeng.springboot.applicationEvent.MyApplicationEvent;
import com.lsqingfeng.springboot.base.Result;
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;
 
/**
 * @className: ApplicationEventController
 * @description:
 * @author: sh.Liu
 * @date: 2022-03-23 15:21
 */
@RestController
@RequestMapping("event")
public class ApplicationEventController {
 
    @Autowired
    private ApplicationContext applicationContext;
 
    @RequestMapping("/push")
    public Result pushEvent(){
        MyApplicationEvent myApplicationEvent = new MyApplicationEvent(this,"zhangsan", 10);
        applicationContext.publishEvent(myApplicationEvent);
        return Result.success();
    }
 
    @RequestMapping("/push3")
    public Result pushEvent2(){
        applicationContext.publishEvent("大家好");
        return Result.success();
    }
}

我們定義兩個推送的方法。一個推送我們的MyApplicationEvent類型,還有一個方法推送一個字串。

當我們呼叫第一個方法的時候,控制台可以列印我們推送的資料資訊。

SpringBoot中ApplicationEvent和ApplicationListener怎麼使用

呼叫推送字串的時候,我們的監聽器不會執行,原因是我們的攔截器裡已經加了泛型MyApplicationEvent,也就是只會監聽MyApplicationEvent類型的消息。其他類型的消息不會被監聽到。

那如果我們把泛型去掉會有什麼效果呢,我們來試試。

SpringBoot中ApplicationEvent和ApplicationListener怎麼使用

每次推送都會發送兩個(可能有什麼內部機制,不管了),但是兩個都列印了,說明如果不加泛型,不管誰推,這邊都能收到訊息。

四、註解方式實現監聽器

除了上面的透過實作介面的方式開發監聽器,我們還可以透過註解的方式來實現,具體程式碼如下。

package com.lsqingfeng.springboot.applicationEvent;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
/**
 * @className: MyApplicationEventListener2
 * @description: 注解实现监听器
 * @author: sh.Liu
 * @date: 2022-03-23 15:56
 */
@Component
public class MyApplicationEventListener2 {
 
    @EventListener
    public void onEvent(MyApplicationEvent event){
        System.out.println("收到消息2:" + event);
    }
}

這裡加入了@EventListener 註解代表了這是一個監聽器。方法名隨意,方法裡的參數代表監聽的事件類型。

再次呼叫push方法:

SpringBoot中ApplicationEvent和ApplicationListener怎麼使用

發現兩個監聽器的資料都會被列印。這一特點大家要注意一下。

以上是SpringBoot中ApplicationEvent和ApplicationListener怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除