1. 程式人生 > >springboot 中事件監聽模式的一種實現

springboot 中事件監聽模式的一種實現

前言: 事件監聽模式是一種常用的設計模式,在springboot 中我們如何實現呢? 首先我們要理解事件監聽中需要的幾個角色

  • 事件釋出者 (即事件源)
  • 事件監聽者
  • 事件本身

廢話不多說直接上程式碼

定義事件本身

事件本身需要繼承ApplicationEvent

package com.yxd;

import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent{

    private String type;
    private List<Map> msg;
    
    public DemoEvent(Object object, String type ,List<Map> msg) {
        super(object);
        this.msg = msg;
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Map> getMsg() {
        return msg;
    }

    public void setMsg(List<Map> msg) {
        this.msg = msg;
    }

}

如圖:

定義事件源

事件源需要注入 ApplicationContext

package com.yxd;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class DemoPublisher {

    @Autowired
    ApplicationContext applicationContext;
    
    public void publish(String type , List<Map> msg) {
        applicationContext.publishEvent(new DemoEvent(this,type, msg ));
    }
}

如圖:

定義監聽者

監聽者需要實現 ApplicationListener

package com.yxd;

import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class DemoListener1 implements ApplicationListener<DemoEvent> {

    @Override
    public void onApplicationEvent(DemoEvent event) {
        List<Map> msg = event.getMsg();
        String type = event.getType();
        System.out.println("listener1 接收到了 publisher 傳送的訊息型別 :" + type +", 訊息內容: " + msg);
    }
}

如圖:

測試

package com.yxd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    DemoPublisher demoPublisher;

    @RequestMapping("testListener")
    public String testListener() {
        ArrayList<Map> list = new ArrayList<>();        
        HashMap<String, String> m1 =  new HashMap<>();
        m1.put("1", "2");
        HashMap<String, String> m2 =  new HashMap<>();
        m2.put("3", "4");
        HashMap<String, String> m3 =  new HashMap<>();
        m3.put("5", "6");       
        list.add(m1);
        list.add(m2);
        list.add(m3);
        demoPublisher.publish("測試訊息",list);
        return "訊息釋出成功";
    }
}

如圖:

我們訪問介面

三個監聽者都得到了訊息。。

專案結構