SpringBoot事件机制的使用(ApplicationEventPublisher、ApplicationEvent)

阅读数:104 评论数:0

跳转到新版页面

分类

python/Java

正文

一、简介

当把一个事件发布到Spring提供的ApplicationContext中,被监听器侦测到,就会执行对理原处理方法。

ApplicationContext继承了ApplicationEventPublisher接口,从而拥有事件发布的能力。但是实际ApplicationContext事件发布委托给ApplicationEventMulticaster执行。

ApplicationEventPublisher 发布事件
ApplicationListener 事件监听者
ApplicationEvent 事件
EventListener 事件和事件监听绑定
ApplicationEventMulticaster 发布事件

二、使用方法

1、自定义发布的事件类,需要继承ApplicationiEvent或其扩展类。

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationEvent;

public class EsSaveEvent extends ApplicationEvent {

    private JSONObject data;

    public EsSaveEvent(JSONObject source) {
        super(source);
        this.data = source;
    }

    public JSONObject getData() {
        return data;
    }
}

2、监听事件

(1)使用注解@EventListener监听事件。

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Component
public class EventListener {

    //监听事件
    @org.springframework.context.event.EventListener
    public void listenEvent(EsSaveEvent  event) {
        //可以添加事务处理
        //divide(event);
        JSONObject object = event.getData();
        //对数据进行处理
         System.out.println("data:"+object.toJSONString());
        }

    }

    //在监听器中重新开一个事务(可选)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void divide(EsSaveEvent event) {
        System.out.println("事务处理");
    }


}

(2)基于ApplicationListener实现事件监听

@Component
public class DemoListener implements ApplicationListener<EsSaveEvent>{
  @Override
  public void onApplicationEvent(EsSaveEvent event){
    ....
  }
}

(3)如果需要异步监听 @Async

参见:Spring注解@EnableAsync @Async

3、使用ApplicationEventPublisher发布事件。

@Autowired
private ApplicationEventPublisher publisher;

public test(){
	JSONObject data=new JSONObject();
    publisher.publishEvent(new EsSaveEvent(data));
}




相关推荐

一、概述 springboot中有两种方式使用kafka时,直接使用kafka-client连接kafka服务;另一种是使用spring-kafka框架来连接kafka。 1、版本兼容 使用时要注意版

当然可以自己写redis的工具类,或者使用第三方开源jar包或代码,这里使用spring boot的集成类。 一、pom依赖 <dependency> <gro

websocket协议基于tcp的网络协议,它实现浏览器与器全双工通信。 spring boot2 +websocket 1、添加依赖 <pre clas

背景: 之前用spring boot+mybatis+oracle,现在要改成spring boot_mybatis+postgresql。 为了想让一套代码即可

共同点 都是用来表示Spring某个类是否可以接收HTTP请求。 不同点 @Controller标识一个spring类是Spring MVC c

系统异常捕获 参见:spring boot 2 全局异常处理 @ControllerAdvice(annotations = {RestController.class}) public class

从SSH(Structs/Spring/Hibernate)到SSM(SpringMVC/Spring/MyBatis),到现在一个S(Spring)就够的年代,可见Spring越来越重要了。<

我们要使用多线程的时候,往往需要创建Thread类,或者实现Runnable接口,如果使用线程池,我们还需要创建Executors,在使用Spring中,只要@EnableAsy

@ConditionalOnMissingBean只能在@Bean注解的方法上使用。 可以给该注解传入参数例如@ConditionOnMissingBean(name = "exa

2.4版本之前 在之前,我们在yaml配置文件中,使用spring.profiles来定义不同环境的标识,比如下面这样: <pre class="language-ma