栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[Java Framework] [Spring] Spring Event / 事件的使用 一: ApplicationEvent

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Java Framework] [Spring] Spring Event / 事件的使用 一: ApplicationEvent

简介

Spring Event 主要是为了解耦代码使用, 对于事件弱相关业务可以添加到事件里面执行
比如: 会员购买会员卡之后会员的状态变更, 发送邮件或者站内信提醒。

JDK的Util包里抽象了事件驱动,有兴趣的朋友可以自行去看下相关类的定义。Spring事件模型ApplicationEvent是基于JDK里的事件模型,

鱗[官方文档] Additional Capabilities of the ApplicationContext

鱗[相关文章] Spring Event / 事件的使用 二: Transactional Event

方法 / 步骤 一: 定义事件实体
  • OrderPayEvent.java
public class OrderPayEvent extends ApplicationEvent {

    
    private Order order;

    public OrderPayEvent(Object source) {
        super(source);
        this.order = (Order) source;
    }

    public Order getOrder() {
        return order;
    }
}
二: 定义监听器

定义监听者的方式,Spring提供了两种,一种是接口方式,一种是注解方式。

2.1: 接口方式

这里定义了两个监听者,实现泛型接口ApplicationListener类型为我们刚定义的OrderPayedEvent这里加上Order注解,是因为我们有多个监听者,有此业务场景中可能会有顺序的要求!

  • OrderPayedPrinterListener.java
@Component
@Order(1)
public class OrderPayedPrinterListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(OrderPayedEvent event) {
        System.out.printf("【线程 - %s 】订单成功成功:第一步,打印小票%n", Thread.currentThread().getName());
    }
}
  • OrderPayedSendMessageListener.java
@Component
@Order(2)
public class OrderPayedSendMessageListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(OrderPayedEvent event) {
        System.out.printf("【线程 - %s 】订单成功成功:第二步,发送通知商品中心添加库存%n", Thread.currentThread().getName());
    }
}
2.1: 注解方式

注解方式是不会有排序功能的,如果你有业务有需要排序,那么建议换成接口方式

@Component
public class OrderPayListener {

    @EventListener(classes = {OrderPayedEvent.class})
    public void sendTips(OrderPayedEvent event) {
        System.out.printf("【线程 - %s 】订单成功成功:发送用户订单支付消息%n", Thread.currentThread().getName());
    }

    @EventListener(classes = {OrderPayedEvent.class})
    public void reward(OrderPayedEvent event) {
        System.out.printf("【线程 - %s 】订单成功成功:奖励业务%n", Thread.currentThread().getName());
    }
}
三: 发送事件
  •  ShoppingMallConsumerApplication.java
@SpringBootApplication
public class ShoppingMallConsumerApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ShoppingMallConsumerApplication.class);
    }

    //这里注入 应用上下文,可以注入 applicationEventPublisher
    @Resource
    ApplicationContext context;
//    @Resource
//    ApplicationEventPublisher applicationEventPublisher;
    @Override
    public void run(String... args) throws Exception {
        context.publishEvent(new OrderPayEvent(new Order(100L,"酸菜鱼")));
    }
}
  • 打印结果
【线程 - main 】订单成功成功:第一步,打印小票
【线程 - main 】订单成功成功:第二步,发送通知商品中心添加库存
异步处理

ApplicationEvent对异步支持是怎么样的呢?

只要在启动类上加上@EnableAsync,在监听器类加上@Async

  •  ShoppingMallConsumerApplication.java
@EnableAsync
@SpringBootApplication
public class ShoppingMallConsumerApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ShoppingMallConsumerApplication.class);
    }

    //这里注入 应用上下文,可以注入 applicationEventPublisher
    @Resource
    ApplicationContext context;
//    @Resource
//    ApplicationEventPublisher applicationEventPublisher;
    @Override
    public void run(String... args) throws Exception {
        context.publishEvent(new OrderPayEvent(new Order(100L,"酸菜鱼")));
    }
}
  • 打印结果
【线程 - task-2 】订单成功成功:第二步,发送通知商品中心添加库存
【线程 - task-1 】订单成功成功:第一步,打印小票
参考资料 & 致谢

[1] Spring事件,ApplicationEvent在业务中的应用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号