一、监听器模式图
二、监听器三要素
三、监听器的实现方式
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 */@Componentpublic 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 */@Componentpublic 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
体验地址:http://www.jnpfsoft.com/?from=infoq
评论