SpringBoot监听器之发布订阅、观察者模式

SpringBoot监听器使用
实现方式有2种:实现ApplicationListener 接口;添加@EventListener 注解。
必须要的角色:
1、事件(event)可以封装和传递监听器中要处理的参数,如对象或字符串,并作为监听器中监听的目标。
2、监听器(listener)具体根据事件发生的业务处理模块,这里可以接收处理事件中封装的对象或字符串。
3、事件发布者(publisher)事件发生的触发者。

接口实现:
自定义事件需要继承Spring的ApplicationEvent
自定义监听器需要实现ApplicationListener
自定义事件发布需要自动注入了ApplicationEventPublisher
注解实现:
自定义事件需要继承Spring的ApplicationEvent
自定义监听器,在自定义的方法上添加@EventListener
自定义事件发布需要自动注入了ApplicationEventPublisher

参考:
https://segmentfault.com/a/1190000039097608

MyEvent

import org.springframework.context.ApplicationEvent;

public class MyEvent extends ApplicationEvent {
    private String msg;

    public MyEvent(Object source) {
        super(source);
    }

    public MyEvent(Object source, String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

MyEventTwo

import org.springframework.context.ApplicationEvent;

public class MyEventTwo extends ApplicationEvent {
    private String msg;

    public MyEventTwo(Object source) {
        super(source);
    }

    public MyEventTwo(Object source, String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

MyAnnotationListener

import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyAnnotationListener {
    @EventListener(classes={MyEvent.class,MyEventTwo.class})
    public void listener(ApplicationEvent applicationEvent){
        if(applicationEvent instanceof MyEvent){
            System.out.println("MyAnnotationListener - MyEvent:"+((MyEvent) applicationEvent).getMsg());
        }

        if(applicationEvent instanceof MyEventTwo){
            System.out.println("MyAnnotationListener - MyEventTwo:"+((MyEventTwo) applicationEvent).getMsg());
        }

    }

    @EventListener
    public void listenerTestOne(MyEvent myEvent){
        System.out.println("listenerTestOne"+myEvent.getMsg());
    }

    @EventListener
    public void listenerTestTwo(MyEventTwo myEventTwo){
        System.out.println("listenerTestTwo"+myEventTwo.getMsg());
    }
}

MyEventPubLisher


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class MyEventPubLisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void pushListener(String msg) {
        applicationEventPublisher.publishEvent(new MyEvent(this, msg));
    }

    public void pushListenerTwo(String msg) {
        applicationEventPublisher.publishEvent(new MyEventTwo(this, msg));
    }

}

ObserverController 测试接口

import com.tech.elasticsearch.observer.MyEventPubLisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/observer")
public class ObserverController {
    @Autowired
    private MyEventPubLisher myEventPubLisher;

    @GetMapping("/publish1")
    public void publisher(String msg){
        myEventPubLisher.pushListener(msg);
    }

    @GetMapping("/publish2")
    public void pushListenerTwo(String msg){
        myEventPubLisher.pushListenerTwo(msg);
    }
}

说明:在MyAnnotationListener类中
如果参数为ApplicationEvent applicationEvent
@EventListener(classes={MyEvent.class,MyEventTwo.class}),则只监听这2个类
@EventListener,则监听所有继承了ApplicationEvent的类

如果参数为MyEvent myEvent或者MyEventTwo myEventTwo
@EventListener,只监听参数类型的事件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,观察者模式被广泛应用于事件驱动的编程模型。Spring Boot通过使用事件和监听器来实现观察者模式,以实现对象之间的解耦和通信。 在Spring Boot中,事件是一个特殊的对象,它封装了与应用程序相关的状态信息。当某个特定的事件发生时,Spring Boot会自动通知所有注册的监听器,并将事件对象传递给它们。监听器可以根据事件的类型和内容来执行相应的操作。 下面是一个简单的示例,演示了如何在Spring Boot中使用观察者模式: 1. 创建一个事件类,例如`MyEvent`,用于封装事件的相关信息。 2. 创建一个监听器类,例如`MyEventListener`,实现`ApplicationListener`接口,并指定要监听的事件类型。 ```java import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class MyEventListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { // 处理事件 System.out.println("Received event: " + event.getMessage()); } } ``` 3. 在需要触发事件的地方,使用`ApplicationEventPublisher`接口的实现类来发布事件。 ```java import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @Component public class MyEventPublisher { private final ApplicationEventPublisher eventPublisher; public MyEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } public void publishEvent(String message) { // 创建事件对象 MyEvent event = new MyEvent(message); // 发布事件 eventPublisher.publishEvent(event); } } ``` 通过以上步骤,我们就可以在Spring Boot中使用观察者模式了。当`MyEventPublisher`发布事件时,`MyEventListener`会自动接收到事件并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值