Spring中@EventListener使用

来自 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 {
    @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";
    }
}

你可能感兴趣的:(Spring,spring,java,后端)