Springboot中自定义监听器

一、监听器模式图

二、监听器三要素

  • 广播器:用来发布事件

  • 事件:需要被传播的消息

  • 监听器:一个对象对一个事件的发生做出反应,这个对象就是事件监听器

三、监听器的实现方式

1、实现自定义事件

自定义事件需要继承ApplicationEvent类,并添加一个构造函数,用于接收事件源对象。该事件中添加了一个SysUser对象,用于传递用户信息。

package com.ruoyi.web.listener;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.ApplicationEvent;

/**
 * @Description: 自定义事件
 * @Author: baiwen
 * @createTime: 2024年06月19日 13:10:07
 */
public class MyEvent extends ApplicationEvent {

    private SysUser sysUser;

    public MyEvent(Object source, SysUser sysUser) {
        super(source);
        this.sysUser = sysUser;
    }

    public SysUser getSysUser() {
        return sysUser;
    }
}

2、实现自定义监听器

自定义监听器需要实现ApplicationListener接口,并重写 onApplicationEvent方法。接口中的泛型参数为自定义事件类型,表示监听该类型的事件。可以从该事件中获取用户信息,并进行相应的处理。

package com.ruoyi.web.listener;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @Description: 自定义监听器
 * @Author: baiwen
 * @createTime: 2024年06月19日 13:12:39
 */
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {
    @Override
    public void onApplicationEvent(MyEvent event) {
        SysUser sysUser = event.getSysUser();
        System.out.println("监听到了事件,用户名:" + sysUser.getUserName());
    }
}

3、发布自定义事件

在需要发布事件的地方,使用ApplicationEventPublisher的publishEvent方法来发布事件。这里使用Test类来模拟事件发布,实际应用中可以根据具体需求来选择合适的发布场景。

package com.ruoyi.test;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.listener.MyEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;

/**
 * @Description:
 * @Author: baiwen
 * @createTime: 2024年06月19日 13:16:33
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyEventPushTest {

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Test
    public void testpublishEvent() throws InterruptedException
    {
        SysUser sysUser = new SysUser();
        sysUser.setUserName("zhangsan");

        System.out.println("发布MyEvent事件。。。");
        applicationEventPublisher.publishEvent(new MyEvent(this, sysUser));
    }
}

4、测试

运行MyEventPushTest类中的testpublishEvent方法,控制台会输出以下内容:

发布MyEvent事件。。。
监听到了事件,用户名:zhangsan

5、其他实现方案

主要是监听器的注册方式不同,目的只有一个,把监听器加入到spring容器中。

方式一,就是上面的MyEventListener类是通过@Component注解将该类注册为Spring的Bean,从而实现监听器的功能。

方式二,可以通过在启动类中添加监听器的方式,使监听器生效。

package com.ruoyi;

import com.ruoyi.web.listener.MyEventListener;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;

/**
 * 启动程序
 * 
 * @author baiwen
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
    public static void main(String[] args)
    {
        new SpringApplicationBuilder(RuoYiApplication.class).listeners(new MyEventListener()).run(args);
    }
}

方式三,可以通过配置spring.factories,使监听器生效。

在resource文件夹下创建META-INF/spring.factories文件。

配置内容如下:

# 监听器
org.springframework.context.ApplicationListener=com.ruoyi.web.listener.MyEventListener

除此之外,还有第四种方式,通过@EventListener注解实现监听器的功能。通过@EventListener注解的condition属性来指定监听的事件类型。

package com.ruoyi.web.listener;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @Description: 自定义监听器2
 * @Author: baiwen
 * @createTime: 2024年06月19日 14:07:57
 */
@Component
public class MyEventListener2 {

    @EventListener(MyEvent.class)
    public void listenerApplicationStarted(MyEvent event) {
        SysUser sysUser = event.getSysUser();
        System.out.println("注解方式监听到了事件,用户名:" + sysUser.getUserName());
    }
}

发布事件后,可以看到能正常监听到事件。

发布MyEvent事件。。。
注解方式监听到了事件,用户名:zhangsan

总结

以上,就是SpringBoot中实现监听器的四种方式。

至于监听器的实现原理,后续再补充。

文章转载自:树叶的一生啊

原文链接:https://www.cnblogs.com/anboy/p/18273370

体验地址:引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

Spring Boot 使用 Flowable 6.8.0 自定义监听器时,可以通过实现 `org.flowable.common.engine.api.delegate.event.FlowableEventListener` 接口,并使用 `@Component` 注解将其注册为 Spring Bean,然后在 `ProcessEngineConfiguration` 设置监听器的优先级。 具体来说,以下是设置监听器优先级的步骤: 1. 创建自定义监听器类,实现 `org.flowable.common.engine.api.delegate.event.FlowableEventListener` 接口,并使用 `@Component` 注解将其注册为 Spring Bean,例如: ```java @Component public class CustomEventListener implements FlowableEventListener { @Override public void onEvent(FlowableEvent event) { // 处理事件 } @Override public boolean isFailOnException() { return false; } } ``` 2. 在 `ProcessEngineConfiguration` 设置监听器的优先级,可以使用 `setEventListeners` 方法,例如: ```java @Bean public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager) { SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration(); // 其他配置... config.setDataSource(dataSource); config.setTransactionManager(transactionManager); // 设置监听器的优先级 Map<String, List<FlowableEventListener>> eventListeners = new HashMap<>(); List<FlowableEventListener> listeners = new ArrayList<>(); listeners.add(customEventListener()); eventListeners.put("TASK_CREATED", listeners); config.setEventListeners(eventListeners); return config; } ``` 在上述代码,通过 `customEventListener()` 方法获取自定义监听器,然后将其添加到 `TASK_CREATED` 事件的监听器列表。`TASK_CREATED` 是一个 Flowable 事件类型,表示任务创建事件。Flowable 还提供了其他的事件类型,可以根据需要设置相应的监听器优先级。 注意,如果需要设置多个事件类型的监听器优先级,可以在 `eventListeners` 添加多个键值对。 另外,如果需要设置多个监听器的优先级,可以将它们添加到同一个事件类型的监听器列表,Flowable 会按照添加的顺序依次触发这些监听器。如果需要改变监听器的顺序,只需要改变它们在列表的位置即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值