基于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、 调用短信发送的接口 -> 恭喜喜提羽绒被子");
}
}
如此,就可以创建多个监听器,进行不同的业务处理。这就是观察者模式。
基于spring的观察者模式的更多相关文章
- 基于Spring Boot/Spring Session/Redis的分布式Session共享解决方案
分布式Web网站一般都会碰到集群session共享问题,之前也做过一些Spring3的项目,当时解决这个问题做过两种方案,一是利用nginx,session交给nginx控制,但是这个需要额外工作较多 ...
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...
- step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework
文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...
- 基于Spring MVC的Web应用开发(三) - Resources
基于Spring MVC的Web应用开发(3) - Resources 上一篇介绍了在基于Spring MVC的Web项目中加入日志,本文介绍Spring MVC如何处理资源文件. 注意到本项目的we ...
- Spring实战5:基于Spring构建Web应用
主要内容 将web请求映射到Spring控制器 绑定form参数 验证表单提交的参数 对于很多Java程序员来说,他们的主要工作就是开发Web应用,如果你也在做这样的工作,那么你一定会了解到构建这类系 ...
- 基于Spring设计并实现RESTful Web Services(转)
基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...
- RESTLET开发实例(三)基于spring的REST服务
http://www.lifeba.org/arch/restlet_spring_3.html 前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将 ...
- [转贴]JAVA:RESTLET开发实例(三)基于spring的REST服务
前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将介绍restlet如何整合spring框架进行开发.Spring 是一个开源框架,是为了解决企业应 ...
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
随机推荐
- Mysql索引优化之索引的分类
Mysql的历史 简单回顾一下Mysql的历史,Mysql 是一个关系型数据库管理系统,由瑞典 Mysql AB 公司开发,目前属于 Oracle 公司.关系型数据库将数据保存在不同的表中,而不是将 ...
- 微服务-springboot打包
idea打包方式: 打包前确认项目可以正常运行 一.File->Project Structure->Artifacts->点击 + ->JAR->From module ...
- mysql 安装使用
本节掌握内容: MySQL的介绍安装.启动 windows上制作服务 MySQL破解密码 MySQL中统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 O ...
- python 实现一个计算器功能
#s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' #第 ...
- 二、JavaScript的语法
目录: 1.变量:存储数据的容器 2.数据类型 3.string数据类型 4.number数据类型 5.boolean数据类型 6.数据类型的隐式转换 6.数据类型转换函数 7.特殊类型 8.算术运算 ...
- Innovus Lab和Lab Guide下载地址 | Innovus教程 - Flow系列 - 数据准备
本文转自:自己的微信公众号<集成电路设计及EDA教程> <Innovus Lab和Lab Guide下载地址 | Innovus教程 - Flow系列 - 数据准备> ...
- Docker笔记(一):什么是Docker
原文地址: http://blog.jboost.cn/2019/07/13/docker-1.html 1. 前言 接触Docker也有两年多了,断断续续玩过一些应用场景,如安装一些常用工具服务, ...
- 手动启动log4j|nginx实现http https共存
手动加载log4j.xml文件 DOMConfigurator.configure("src/main/resources/log4j.xml"); log4j.propertie ...
- 生成数据库自增不重复ID的方法
namespace ConsoleApp1 { class Program { static void Main(string[] args) { ); ; i < ; i++) { var t ...
- 关于使用 AJax 生成Form表单,且表单提交需要验证,验证实效的解决方法
@Ajax.ActionLink("添加", "AddUser",new AjaxOptions() {InsertionMode = InsertionMod ...