《Spring之事件监听模式》

在日常开发中,普遍会遇到以下场景,比如电商平台 下单成功与发送短信 一般我们会做成两个服务 通过一些http请求 或者mq这种实现解耦 ,就是说不会让其中一个服务失败而影响另外一个。

今天我们用Spring事件监听的方式来实现:

   监听的四要素

        1.事件源:我或者其他触发的目标 比如鼠标(或者下单服务)

        2.事件:比如鼠标点击(下单成功)

        3.监听器:监听鼠标并做相应的操作(监听下单成功并发送短信)

        4.发布器:发布事件源(发布下单成功通知)

 

下面上代码:

package com.example.ertongtest.eventListen;

/**
 *  定义一个下订单对象 封装下订单的数据
 **/

public class Order {
    private  Integer id ;

    private String name ;

    public Order(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Order() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.ertongtest.eventListen;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    OrderService orderService ;

    @GetMapping("/buy")
    public  Order buy(){
        Order order = new Order(123,"RTX3898");
        orderService.buy(order);
        return  order ;
    }
}
package com.example.ertongtest.eventListen;

public interface OrderService {
    void buy(Order order);
}
package com.example.ertongtest.eventListen;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService, ApplicationEventPublisherAware {
    /**
     * 发布器
     * */
   private ApplicationEventPublisher publisherAware ;

    @Override
    public void buy(Order order) {
        System.out.println("下单成功");
        publisherAware.publishEvent(new OrderSucessEvent(this,order));
        System.out.println("不影响下一步操作");
        System.out.println("不影响下一步操作");
        System.out.println("不影响下一步操作");
        System.out.println("不影响下一步操作");

    }


    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisherAware = applicationEventPublisher ;
    }
}
package com.example.ertongtest.eventListen;
/*
* 事件源 下单成功
* */

import org.springframework.context.ApplicationEvent;

public class OrderSucessEvent extends ApplicationEvent {
    private  Order order ;

    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public OrderSucessEvent(Object source) {
        super(source);
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public OrderSucessEvent(Object source, Order order) {
        super(source);
        this.order = order;
    }
}
package com.example.ertongtest.eventListen;

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

/**
 *
 * 监听器
 */
@Component
public class SmsListener implements ApplicationListener<OrderSucessEvent> {
    @Override
    public void onApplicationEvent(OrderSucessEvent event) {
        System.out.println(event.getOrder());
        System.out.println(event.getSource());
        System.out.println(event.getTimestamp());
        System.out.println("发送短信成功");
    }
}

还可以使用SpringBoot的自动装配方式实现监听器 灰常的方便与快捷:

 小提示 可以在condition注解里面使用#语法方式 实现条件匹配

package com.example.ertongtest.eventListen;

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

@Component
public class CountSalesListentener {

    @EventListener(condition = "#event.order.name.equals('ipone12')")
    public  void countSales(OrderSucessEvent event){
        System.out.println("countSales:"+event.getOrder());
    }
}

如果同一个事件源 有多个监听器在监听 如何保证顺序,也是非常的简单 加个@Order注解即可 对Spring的Bean排序的作用 本身监听器也是Spring的一个Bean 上代码:

package com.example.ertongtest.eventListen;

import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
public class CountSalesListentener {
    @Order(1)
    @EventListener(condition = "#event.order.name.equals('ipone12')")
    public  void countSales1(OrderSucessEvent event){
        System.out.println("countSales1:"+event.getOrder());
    }
    @Order(2)
    @EventListener(condition = "#event.order.name.equals('ipone12')")
    public  void countSales2(OrderSucessEvent event){
        System.out.println("countSales2:"+event.getOrder());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

森林里的程序猿猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值