Spring中@EventListener使用

文章介绍了如何在Spring框架中使用@EventListen进行事件监听。通过创建自定义事件,使用ApplicationContext发布事件,以及实现ApplicationListener接口来接收事件。示例代码展示了在Controller中触发事件,以及在Listener中处理事件的方法。
摘要由CSDN通过智能技术生成

来自 https://blog.csdn.net/flymoringbird/article/details/120481883 的个人理解

EventListen简单实现

@EventListen 可以理解为一个广播事件

Controller

@RestController
public class TestController {

    @Resource
    ApplicationContext applicationContext;

    @RequestMapping("/hello")
    public String getContent(){
        String hello = "hello";
        applicationContext.publishEvent(hello);
        return "success";
    }
}

Listener

@Component
public class HelloEventListener {
    @EventListener
    public void onApplicationEvent(String str){
        System.out.println(str);
    }

    @EventListener
    public void onApplicationEvent1(String str){
        System.out.println(str + '1');
    }
}

ApplicationEventMulticaster + ApplicationEvent + ApplicationListener

类似接口开发

创建自定义事件(用于发送 和 接收时识别)

需要继承ApplicationEvent,用于自定义

public class test1 extends ApplicationEvent {

    /**
     *
     * @param source  事件源
     * @param orderId 订单id
     */
    public test1(Object source, Long orderId) {
        super(source);
        this.orderId = orderId;
    }

    private Long orderId;
    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }
}

监听器

需要实现 ApplicationListener 来指定接收

@Component
public class HelloEventListener implements ApplicationListener<test1> {
    @Override
    public void onApplicationEvent(test1 event) {
        Long orderId = event.getOrderId();
        System.out.println(orderId + "1");
    }
}

controller

先创建 applicationEventMulticaster
然后添加监听器,不添加接收不到
通过multicastEvent 进行发送

 @RequestMapping("/hello")
 public String getContent(){
      ApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster();
      applicationEventMulticaster.addApplicationListener(new HelloEventListener());
      applicationEventMulticaster.addApplicationListener(new HelloEventListener1());
      //test2 test2 = new test2(applicationEventMulticaster, 123L);
      test1 test1 = new test1(applicationEventMulticaster, 123L);
      applicationEventMulticaster.multicastEvent(test1);
      return "success";
  }

ApplicationContext + ApplicationEventMulticaster + @EventListener

注解开发

创建自定义事件

与上面一样
需要继承ApplicationEvent,用于自定义

public class test2 extends ApplicationEvent {

    public test2(Object source, Long orderId) {
        super(source);
        this.orderId = orderId;
    }

    private Long orderId;
    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }
}

监听器

@Component
public class HelloEventListener2 {
    @EventListener
    public void onApplicationEvent(test2 test1){
        System.out.println(test1.getOrderId() + "3");
    }
}

controller

如果不使用ApplicationEvent自定义发送,导致类型不同而报错

@RestController
public class Test1Controller {
    @Resource
    ApplicationContext applicationContext;

    @RequestMapping("/hello1")
    public String getContent(){

        ApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster();
        test2 test2 = new test2(applicationEventMulticaster, 123L);

        applicationContext.publishEvent(test2);

        return "success1";
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值