使用SpringEvent事件发布监听机制来解耦你的代码

​欢迎光临我的博客查看最新文章: https://river106.cn

Spring Event是Spring的事件通知机制,可以将相互耦合的代码解耦,从而方便功能的修改与添加。Spring Event是观察者模式的一个具体实现。
Spring Event包含事件(ApplicationEvent)、监听器(ApplicationListener)和事件发布(publishEvent)操作。

Spring Event的相关API在spring-context包中;

实现Spring事件机制主要有4个类:

ApplicationEvent:事件,每个实现类表示一类事件,可携带数据。
ApplicationListener:事件监听器,用于接收事件处理时间。
ApplicationEventMulticaster:事件管理者,用于事件监听器的注册和事件的广播。
ApplicationEventPublisher:事件发布者,委托ApplicationEventMulticaster完成事件发布。

以下示例中使用SpringBoot。

1、引入Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2、自定义事件

Spring提供了事件抽象类ApplicationEvent,继承该抽象类即可。

import org.springframework.context.ApplicationEvent;

@Setter
@Getter
public class MsgEvent extends ApplicationEvent {
    private String message;

    public MsgEvent(String message) {
        super(message);
        this.message = message;
    }
}

3、发布事件

1、可以直接使用ApplicationEvent.publishEvent(ApplicationEvent);

@Autowired
private ApplicationContext applicationContext;
applicationContext.publishEvent(ApplicationEvent);

2、通过ApplicationEventPublisher.publishEvent(ApplicationEvent);
直接注入

@Autowired
private ApplicationEventPublisher applicationEventPublisher;
applicationEventPublisher.publishEvent(ApplicationEvent)

实现接口

@Service
public class PublishEvent implements ApplicationEventPublisherAware {
    public static ApplicationEventPublisher eventPublisher = null;
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        eventPublisher.publishEvent("事件消息");
    }
}

4、事件监听

处理发布的事件
1、通过实现接口ApplicationListener

@Component
public class MsgEventListener implements ApplicationListener<MsgEvent> {
    @Override
    public void onApplicationEvent(TestEvent event) {
        // 事件处理
        System.out.println("监听到消息: " + event.getMessage());
    }
}

2、使用@EventListener注解

@Component
public class MsgEventListener {
    
    @Order(5)
    @EventListener
    public void listener(MsgEvent event) {
        System.out.println("注解监听到数据:" + event.getMessage());
    }
}

如果有多个监听器,可以通过@Order注解指定监听器执行顺序,@Order中value的值越小,越先执行;
事件监听器默认是同步执行的,如果想要异步执行,可以添加@Async注解(启动类上加@EnableAsync),实际业务开发中要注意@Async的使用。

5、测试事件发布监听

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("cn.river.spring.event")
public class ApplicationListenerDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationListenerDemo.class);
        context.publishEvent("123123");

        MsgEvent msgEvent = new MsgEvent("发送短信");
        context.publishEvent(msgEvent);
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值