SpringBoot实现自定义事件的方法详解

目录
  • 简介
  • 步骤1:自定义事件
  • 步骤2:自定义监听器
    • 方案1:ApplicationListener
    • 方案2:SmartApplicationListener
  • 步骤3:注册监听器
    • 法1:@Component(适用于所有监听器)
    • 法2:application.yml中添加配置
    • 法3:启动类中注册
  • 步骤4:发布事件
    • 法1:注入ApplicationContext,调用其publishEvent方法
    • 法2:启动类中发布

简介

说明

本文用实例来介绍如何在SpringBoot中自定义事件来使用观察者模式。

事件的顺序

可使用实现Ordered接口的方式,调整监听器顺序。

注意:必须是同时实现 ApplicationListener<MyEvent>,Ordered这样的方法才能控制顺序。

下边几种都是无法控制顺序的:

  • @Component+@EventListerner+实现Ordered
  • 实现 ApplicationListener<MyEvent>+@Order

步骤1:自定义事件

通过继承ApplicationEvent来自定义事件。

构造器的参数为该事件的相关数据对象,监听器可以获取到该数据对象,进而进行相关逻辑处理。

package com.example.event;

import org.springframework.context.ApplicationEvent;

public class MyEvent extends ApplicationEvent {

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

}

步骤2:自定义监听器

方案1:ApplicationListener

法1:@EventListener

监听单个事件

package com.example.event;

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

public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("监听器:     " + "MyListener");
        System.out.println("  线程:     " + Thread.currentThread().getName());
        System.out.println("  事件:     " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

或者

package com.example.event;

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

public class MyListener {
    @EventListener({MyEvent.class})
    public void abc(ApplicationEventevent) {
        System.out.println("监听器:      " + "MyListener");
        System.out.println("  线程:      " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据: " + event.getSource());
    }
}

上边的办法比较好,因为不需要类型转换了。直接就能确定是MyEvent类型。

监听多个事件

事件进来之后,可以使用if(event instanceOf MyEvent.class)来判断是哪种事件。

package com.example.event;

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

@Component
public class MyListener {
    @EventListener({MyEvent.class, MyEvent2.class})
    public void abc(ApplicationEvent event) {
        System.out.println("监听器:      " + "MyListener");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

监听所有ApplicationEvent

若使用这种写法,启动时会打印很多Spring自带的事件。任意ApplicationEvent都会进入这里边。

package com.example.event;

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

@Component
public class MyListener {
    @EventListener
    public void abc(ApplicationEvent event) {
        System.out.println("监听器:     " + "MyListener");
        System.out.println("  所在线程: " + Thread.currentThread().getName());
        System.out.println("  事件:     " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

法2:实现ApplicationListener<T>接口

public class MyListener implements ApplicationListener<MyEvent>{

    public void onApplicationEvent(MyEvent event){
        System.out.println("监听器:     " + "MyListener");
        System.out.println("  所在线程: " + Thread.currentThread().getName());
        System.out.println("  事件:     " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

方案2:SmartApplicationListener

源码如下

public interface SmartApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered {
    boolean supportsEventType(Class<? extends ApplicationEvent> var1);

    default boolean supportsSourceType(@Nullable Class<?> sourceType) {
        return true;
    }

    default int getOrder() {
        return 2147483647;
    }
}

supportsEventType:支持的事件的类型

supportsSourceType:支持的数据的类型

getOrder:2147483641:是2^31-1,也就是32位的int的最大正数 。

示例

事件

package com.example.event;

import org.springframework.context.ApplicationEvent;

public class MyEvent extends ApplicationEvent {

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

}

监听器1

package com.example.event;

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

@Component
public class MyListener implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return aClass == MyEvent.class;
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        return sourceType == String.class;
    }

    @Override
    public int getOrder() {
        return 2;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("监听器:      " + "MyListener");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
        System.out.println("  是MyEvent?:" + (event instanceof MyEvent));
    }
}

监听器2

package com.example.event;

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

@Component
public class MyListener2 implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return aClass == MyEvent.class;
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        return sourceType == String.class;
    }

    @Override
    public int getOrder() {
        return 1;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("监听器:      " + "MyListener2");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
        System.out.println("  是MyEvent?:" + (event instanceof MyEvent));
    }
}

发布器

package com.example.event;

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

@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;

    public void myPublish(String message) {
        System.out.println("发布器所在线程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

测试

package com.example.controller;

import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;

    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }

}

启动后,访问:http://localhost:8080/test1

后端输出:

发布器所在线程:http-nio-8080-exec-2
监听器:      MyListener2
  所在线程:  http-nio-8080-exec-2
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
  是MyEvent?:true
监听器:      MyListener
  所在线程:  http-nio-8080-exec-2
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
  是MyEvent?:true

如果将监听器的实现的Ordered顺序颠倒,则输出结果如下:

发布器所在线程:http-nio-8080-exec-1
监听器:      MyListener
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
  是MyEvent?:true
监听器:      MyListener2
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
  是MyEvent?:true

步骤3:注册监听器

方式 适用范围 能否搭配@Async注解,进行异步监听
@Component 所有监听器
application.yml中添加配置 实现ApplicationListener<T>接口的监听器 不能
启动类中注册 实现ApplicationListener<T>接口的监听器 不能

法1:@Component(适用于所有监听器)

package com.example.event;

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

@Component
public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("监听器:" + "MyListener");
        System.out.println("线程:" + Thread.currentThread().getName());
        System.out.println("事件:" + event);
        System.out.println("事件的数据:" + event.getSource());
    }
}
package com.example.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyListener2 implements ApplicationListener<MyEvent> {

    @Override
    public void onApplicationEvent(MyEvent event) {
        System.out.println("监听器:" + "MyListener2");
        System.out.println("线程:" + Thread.currentThread().getName());
        System.out.println("事件:" + event);
        System.out.println("事件的数据:" + event.getSource());
    }
}

法2:application.yml中添加配置

只适用于实现ApplicationListener<T>接口的监听器

context:
  listener:
    classes: com.example.event.MyListener,com.example.event.MyListener2

法3:启动类中注册

只适用于实现ApplicationListener<T>接口的监听器

package com.example;

import com.example.event.MyListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        // 原来是这样的:SpringApplication.run(DemoApplication.class, args);
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        context.addApplicationListener(new MyListener());
    }
}

步骤4:发布事件

法1:注入ApplicationContext,调用其publishEvent方法

package com.example.event;

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

@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;

    public void myPublish(String message) {
        applicationContext.publishEvent(new MyEvent(message));
    }
}

法2:启动类中发布

package com.example;

import com.example.event.MyEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        //原来是:SpringApplication.run(DemoApplication.class, args);
        ConfigurableApplicationContext context =SpringApplication.run(DemoApplication.class, args);
        context.publishEvent(new MyEvent("Hello"));
    }
}

以上就是SpringBoot实现自定义事件的方法详解的详细内容,更多关于SpringBoot自定义事件的资料请关注我们其它相关文章!

(0)

相关推荐

  • SpringBoot实现异步事件驱动的方法

    在项目实际开发过程中,我们有很多这样的业务场景:一个事务中处理完一个业务逻辑后需要跟着处理另外一个业务逻辑,伪码大致如下: @Service public class ProductServiceImpl { ... public void saveProduct(Product product) { productMapper.saveOrder(product); notifyService.notify(product); } ... } 很简单并且很常见的一段业务逻辑:首先将产品先保存数

  • springboot 事件监听器的案例详解

    目录 前言 引导案例 一.通过实现ApplicationListener接口实现步骤 1.自定义一个事件类(对象),继承ApplicationEvent 2.自定义业务类实现ApplicationListener 接口 3.主线业务发布事件 二.通过添加 @EventListener 注解来实现 三.使用异步 前言 在spring框架中,提供了很多动态灵活且可扩展的机制,开发者可以利用这些机制完成一些巧妙的业务,实现一些业务中的解耦, 引导案例 下面看一个简单的案例, @Configuratio

  • SpringBoot事件机制相关知识点汇总

    要"监听"事件,我们总是可以将"监听器"作为事件源中的另一个方法写入事件,但这将使事件源与监听器的逻辑紧密耦合. 对于实际事件,我们比直接方法调用更灵活.我们可以根据需要动态注册和注销某些事件的侦听器.我们还可以为同一事件设置多个侦听器. 本教程概述了如何发布和侦听自定义事件,并解释了 Spring Boot 的内置事件. 为什么我应该使用事件而不是直接方法调用? 事件和直接方法调用都适合于不同的情况.使用方法调用,就像断言一样-无论发送和接收模块的状态如何,他们都

  • springboot 事件监听的实现方法

    定义事件 @Getter public class TestEvent extends ApplicationEvent { private String msg; public TestEvent(Object source, String msg) { super(source); this.msg = msg; } } 定义事件监听(注解方式) @Component public class TestListen { @EventListener public void testListe

  • Spring boot事件监听实现过程解析

    事件监听其实我们并不陌生,简单来讲,当程序达到了某个特定的条件,程序就会自动执行一段指令.在spring 中也一样,我们可以使用spring中的事件监听来实现某些特定的需求. 发布事件 既然要监听事件,首先要发布我们的事件嘛.在spring中发布事件我们可以通过继承ApplicationEvent 来发布我们的事件类. @Data public class SendEvent extends ApplicationEvent { public SendEvent(Object source) {

  • SpringBoot实现自定义事件的方法详解

    目录 简介 步骤1:自定义事件 步骤2:自定义监听器 方案1:ApplicationListener 方案2:SmartApplicationListener 步骤3:注册监听器 法1:@Component(适用于所有监听器) 法2:application.yml中添加配置 法3:启动类中注册 步骤4:发布事件 法1:注入ApplicationContext,调用其publishEvent方法 法2:启动类中发布 简介 说明 本文用实例来介绍如何在SpringBoot中自定义事件来使用观察者模式

  • SpringBoot进行参数校验的方法详解

    目录 介绍 1.SpringBoot中集成参数校验 1.1引入依赖 1.2定义参数实体类 1.3定义校验类进行测试 1.4打开接口文档模拟提交数据 2.参数异常加入全局异常处理器 3.自定义参数校验 3.1创建自定义注解 3.2自定义校验逻辑 3.3在字段上增加注解 3.4体验效果 4.分组校验 4.1定义分组接口 4.2在模型中给参数分配分组 4.3体现效果 介绍 在日常的接口开发中,为了防止非法参数对业务造成影响,经常需要对接口的参数进行校验,例如登录的时候需要校验用户名和密码是否为空,添加

  • SpringBoot统一返回格式的方法详解

    目录 前言 1. 直接返回结果 2. 约定返回格式 3. 返回统一格式结果 4. 切片封装统一格式 编写注解 编写ControllerAdvice 见证奇迹的时刻到了 5. 自定义返回格式 场景1:返回成功时code为200 场景2:自定义返回格式 前言 目前很多项目都是前后端分离,前后端会事先约定好返回格式.那么后端如何做,才能优雅的返回统一格式呢,接下来,请大家跟着我,一步步来实现. 1. 直接返回结果 先看一下最基本的例子,直接将结果原封不动返回: @Data @AllArgsConstr

  • Jsp自定义标签和方法详解

    Jsp自定义标签和方法详解 首先是要有一个标签处理类,标签处理类可以直接实现Tag接口,也可以继承Java中已经实现了的TagSupport这个类,TagSupport也是继承自Tag接口的,它内部已经对Tag接口进行了实现,一般是继承TagSupport类,之后是重写父类的doStartTag和doEndTag方法, 对于开始标签来说返回值主要有EVAL_BODY_INCLUDE和SKIP_BODY,前者表示执行标签体,后者表示略过标签体: 对于结束标签的返回值主要有两种EVAL_PAGE和S

  • Android ListView监听滑动事件的方法(详解)

    ListView的主要有两种滑动事件监听方法,OnTouchListener和OnScrollListener 1.OnTouchListener OnTouchListener方法来自View中的监听事件,可以在监听三个Action事件发生时通过MotionEvent的getX()方法或getY()方法获取到当前触摸的坐标值,来对用户的滑动方向进行判断,并可在不同的Action状态中做出相应的处理 mListView.setOnTouchListener(new View.OnTouchLis

  • 对python 自定义协议的方法详解

    前面说到最近在写python的一些东西,然后和另外一位小伙伴定义了协议,然后昨天我有一部分东西没理解对,昨天上午我自己重写了一遍接收和发送的全部逻辑,昨天下午补了压力测试的脚本,自测没问题之后告知联调的小伙伴. 结果上午还是出了一点问题,然后我们两对代码,他写了一个python的实现.还好最后我这边没问题.(我也害怕是我这边出问题啊,所以我自己的代码都自己检查了好几遍) 简单放一下他的实现: import struct import ctypes class E(Exception): def

  • SpringBoot Jpa 自定义查询实现代码详解

    这篇文章主要介绍了SpringBoot Jpa 自定义查询实现代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 持久层Domain public interface BaomingDao extends JpaRepository<BaomingBean,Integer> { @Query(value = "select distinct t.actid from BaomingBean t where t.belongs=?

  • springboot命令行启动的方法详解

    springboot命令行启动 创建的springboot项目想看看效果,不想打开idea等开发工具,使用直接使用命令行启动. maven的命令启动 需要将 jdk的bin目录和maven的bin目录添加到环境变量path中,若是没有,mvn就要用在maven的bin环境中的全路径 若是没有添加环境变量 mvn就要是E:\software\apache-maven-3.3.9\bin\mvn(安装路径\bin\mvn) java就要是C:\software\jdk\bin\java.exe(安装

  • Mockito 结合 Springboot 进行应用测试的方法详解

    Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven Development)工具.Cucumber和Spock协同工作,对应用程序进行测试. 在web应用程序中,我们主要是对Service层做单元测试,以前单元测试都是使用 junit4 ,对Controller层做集成测试或者接口测试,对Controller层的测试一般有两种方法:(1)发送htt

  • C++ STL priority_queue自定义排序实现方法详解

    前面讲解 priority_queue 容器适配器时,还遗留一个问题,即当 <function> 头文件提供的排序方式(std::less<T> 和 std::greater<T>)不再适用时,如何自定义一个满足需求的排序规则. 首先,无论 priority_queue 中存储的是基础数据类型(int.double 等),还是 string 类对象或者自定义的类对象,都可以使用函数对象的方式自定义排序规则.例如: #include<iostream> #in

随机推荐