• 基于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、 调用短信发送的接口 -> 恭喜喜提羽绒被子");
        }
    }

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

  • 相关阅读:
    sql server中sql语句中单引号怎么转义?【转】
    如何配置Eclipse+Tomcat 开发环境【转】
    JBoss和Tomcat版本、及Servlet、JSP规范版本对应一览 【转】
    P1955 [NOI2015]程序自动分析 && 离散化学习 && lower_bound学习
    P1604 B进制星球
    [OI
    [OI
    Aiyi's Code Style for OI
    POJ1186 方程的解数
    [Daily Life]百首好歌
  • 原文地址:https://www.cnblogs.com/itbac/p/10921041.html
Copyright © 2020-2023  润新知