当前位置: 首页 > news >正文

@Autowired搭配@interface注解实现策略模式

应用场景:存在银行卡和社保卡的支付、退货等接口,接口报文中使用transWay表示银行卡(0)和社保卡(1),transType表示支付(1)、退货(2)。那么由其组合便能出现四个逻辑,所以要实现动态的逻辑分发。

domain

@Data
public class PosApiReq {/** 交易方式 **/private String transWay;/** 交易类型 **/private String transType;
}

service

  • 接口定义
public interface ICommonService {public Object handler(String json);
}
  • 银行卡消费
@Service
@CodeType("01")
public class BankConsumService implements ICommonService{@Overridepublic Object handler(String json) {System.out.println("银行卡消费开始:" + json);return "bank-consum";}
}
  • 银行卡退货
@Service
@CodeType("02")
public class BankRefundService implements ICommonService{@Overridepublic Object handler(String json) {System.out.println("银行卡退货开始:" + json);return "bank-refund";}
}
  • 社保卡消费
@Service
@CodeType("11")
public class SocConsumService implements ICommonService{@Overridepublic Object handler(String json) {System.out.println("社保卡消费开始:" + json);return "soc-consum";}
}
  • 社保卡退货
@Service
@CodeType("12")
public class SocRefundService implements ICommonService{@Overridepublic Object handler(String json) {System.out.println("社保卡退货开始:" + json);return "soc-refund";}
}

controller

@RestController
public class ServerController {private final Map<String, ICommonService> handlerMap = new HashMap<>();@Autowiredprivate void setHandler(List<ICommonService> commonServiceList) {for (ICommonService commonService : commonServiceList) {handlerMap.put(commonService.getClass().getAnnotation(CodeType.class).value(), commonService);}}@Anonymous@RequestMapping("/api")public Object api(@RequestBody PosApiReq posApiReq) {String transWay = posApiReq.getTransWay();String transType = posApiReq.getTransType();// 01 银行卡消费、02银行卡退货、11 社保卡消费、 12社保卡退货String value = transWay+transType;ICommonService commonService = handlerMap.get(value);Object object = commonService.handler(posApiReq.toString());return object;}
}

annotation

@Target(value={ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CodeType {String value();
}

此方式完美的解决了根据不通交易调用不通逻辑的问题,如果新添加一类交易,只需要扩展实现ICommonService 接口的新类就可以。

http://www.lryc.cn/news/264175.html

相关文章:

  • Linux CentOS下Composer简单使用
  • Mysql-干净卸载教程
  • 纵横字谜的答案 Crossword Answers
  • cpp_04_类_对象_this指针_常对象_常(成员)函数
  • AttributeError: module ‘_winapi‘ has no attribute ‘SYNCHRONIZE‘解决方案
  • 多媒体互动橱窗设计如何改变内容展示形式?
  • flutter自定义地图Marker完美展示图片
  • no module named cv2 、numpy 、xxx超全解决方案
  • BSWM 模式管理(二)ESH
  • mac电脑安装虚拟机教程
  • 手动配置 kafka 用户密码,认证方式等的方式
  • 机器学习 深度学习 神经网络
  • VCG 获取某个顶点的邻接顶点
  • 四川云汇优想教育咨询有限公司电商服务靠谱吗
  • Spring MVC框架支持RESTful,设计URL时可以使用{自定义名称}的占位符@Get(“/{id:[0-9]+}/delete“)
  • 【GoLang】哪些大公司正在使用Go语言
  • 美团外卖商超商品销量数据
  • 【C++高阶(八)】单例模式特殊类的设计
  • Linux之进程(五)(进程控制)
  • 63. 不同路径 II 23.12.21(二)
  • 【线性代数】两个向量组等价,其中一个向量组线性无关,另一个向量组也是线性无关吗?
  • c语言:指针作为参数传递
  • YOLOv5性能评估指标->mAP、Precision、Recall、FPS、Confienc (讲解论文关注的主要指标)
  • 陶建辉在 CIAS 2023 谈“新能源汽车的数字化”
  • PSP - 结构生物学中的机器学习 (NIPS MLSB Workshop 2023.12)
  • 某领先的集成电路研发中心:建立跨网交换平台 杜绝数据泄露风险
  • map|动态规划|单调栈|LeetCode975:奇偶跳
  • 从安全性角度,看“可信数字底座”有何价值
  • 软件设计模式:UML类图
  • 力扣题目学习笔记(OC + Swift)15. 三数之和