zoukankan      html  css  js  c++  java
  • 基于spring的观察者模式

      简单的说,观察者模式,就类似于 广播站发送广播,和收音机的关系。多个收音机去收听同一个广播频道。 在实际的业务场景中,可以是这样的。创建订单成功后,发布事件。然后减库存。发送短信。调用微信。调用物流服务。等多个后续业务,都去监听同一个事件。

      定义一个事件。

    package com.study.design.observer.spring;
    
    import org.springframework.context.ApplicationEvent;
    
    /**
     * 定义事件
     */
    
    public class OrderEvent extends ApplicationEvent {
        public OrderEvent(Object source) {
            super(source);
        }
    }

      定义事件发布器

    package com.study.design.observer.spring;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.stereotype.Service;
    
    // 事件发布器。把事件发布到 spring容器中。
    
    @Service
    public class OrderPublish implements ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            // 获取spring容器,设置到私有属性。
            this.applicationContext = applicationContext;
        }
    
        // 调用spring容器 发布事件
        public void publishEvent(ApplicationEvent event){
            applicationContext.publishEvent(event);
        }
        
    }

      订单服务中,发布事件

    package com.study.design.observer.spring;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * 订单操作,业务伪代码
     */
    @Service
    public class OrderService {
        
        // 注入事件发布器
        @Autowired
       private OrderPublish orderPublish;
    
        /**
         * 电商 - 新订单订单
         */
        public void saveOrder() {
    
            System.out.println("1、 订单创建成功");
    
            // 创建事件 ,可以设置参数
            OrderEvent orderEvent = new OrderEvent(123456);
    
            // 发布事件
            orderPublish.publishEvent(orderEvent);
    
        }
    }

      发短信  监听器 服务

    package com.study.design.observer.spring;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    
    // 监听器 ,有事件发布后,才会执行
    @Component
    public class SmsListener implements ApplicationListener<OrderEvent> {
        @Override
        public void onApplicationEvent(OrderEvent event) {
    
            // 获取事件中的参数。
            System.out.println("event.getSource的值是:"+event.getSource());
    
            // 2---短信通知
            System.out.println("2、 调用短信发送的接口 -> 恭喜喜提羽绒被子");
        }
    }

      如此,就可以创建多个监听器,进行不同的业务处理。这就是观察者模式。

  • 相关阅读:
    Webx5 label的取值和赋值(div)
    webx5 复选框的显示
    WebX5 Data遍历以及获取数组最后一个元素(更新)
    API集市--分享API
    WebX5 Data判断当前行的值是否改变,以及改变当前行的状态
    WebX5手机GPS定位,无需开启网络
    JS:var 变量=变量 && 变量
    jQuery的ready ()的几种写法
    .net 微信支付(公众号支付)遇到的问题
    .net中的Dictionary字典类的使用方法
  • 原文地址:https://www.cnblogs.com/itbac/p/10921041.html
Copyright © 2011-2022 走看看