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

一个接口多个实现类,如何动态调用

方法一:使用ApplicationContext(也可以借助配置文件)

1、一个支付接口

package com.niuniu.order.service.pay;public interface PayService {String pay();
}

2、两种支付方法实现类

package com.niuniu.order.service.pay.impl;import com.niuniu.order.service.pay.PayService;
import org.springframework.stereotype.Service;@Service
public class ZfbPayServiceImpl implements PayService {@Overridepublic String pay() {return "zfb";}
}
package com.niuniu.order.service.pay.impl;import com.niuniu.order.service.pay.PayService;
import org.springframework.stereotype.Service;@Service
public class WeiXinPayServiceImpl implements PayService {@Overridepublic String pay() {return "weixin";}
}

3、自定义MyServiceFactory类

package com.niuniu.order.service.pay;import com.niuniu.order.service.pay.impl.WeiXinPayServiceImpl;
import com.niuniu.order.service.pay.impl.ZfbPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;@Service
public class MyServiceFactory {@Autowiredprivate ApplicationContext applicationContext;public PayService getPayService(String beanName){switch (beanName){case ("zfbPayServiceImpl") :return applicationContext.getBean(ZfbPayServiceImpl.class);case ("weiXinPayServiceImpl") :return applicationContext.getBean(WeiXinPayServiceImpl.class);default:throw new IllegalArgumentException("Unknown service type!");}}
}

4、Controller调用

@Autowiredprivate MyServiceFactory myServiceFactory;@PostMapping("/pay")public Response pay(){
//        String s = myServiceFactory.getPayService("zfbPayServiceImpl").pay();String s = myServiceFactory.getPayService("weiXinPayServiceImpl").pay();return Response.ok(s);}

postman测试

方法二:使用Map存储各实现类的实例(策略模式)

package com.niuniu.order.service.pay;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Map;@Service
public class MyServiceFactory2 {/*** key是bean名称*/private final Map<String, PayService> payServiceMap;@Autowiredpublic MyServiceFactory2(Map<String, PayService> payServiceMap) {this.payServiceMap = payServiceMap;}public PayService getPayService(String beanName){return payServiceMap.get(beanName);}
}

controller调用:

@Autowiredprivate MyServiceFactory2 myServiceFactory2;@PostMapping("/pay")public Response pay(){String s = myServiceFactory2.getPayService("zfbPayServiceImpl").pay();
//        String s = myServiceFactory2.getPayService("weiXinPayServiceImpl").pay();return Response.ok(s);}

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

相关文章:

  • 长篇音频制作(小说自动配音)完整教程
  • 15.卷积神经网络
  • 【题解】[CQOI2006] 洛谷P4196 凸多边形 /【模板】半平面交
  • 钻井泥浆搅拌机的设计cad1张三维图+设计说明书
  • 【Abp.VNext】Abp.Vnext框架模块学习
  • 服务器如何应对SYN Flood攻击?
  • 如何管理需求文档的版本历史
  • 【嵌入式电机控制#31】FOC:霍尔安装误差的补偿
  • MyBatis 中 XML 与 DAO 接口的位置关系及扫描机制详解
  • 深度学习——03 神经网络(2)-损失函数
  • Flutter网络请求实战:Retrofit+Dio完美解决方案
  • 51单片机-51单片机最小系统
  • 区块链DApp:颠覆未来的去中心化应用
  • 性能优化之通俗易懂学习requestAnimationFrame和使用场景举例
  • PyTorch生成式人工智能——基于Transformer实现文本转语音
  • 浅谈TLS 混合密钥交换:后量子迁移过渡方案
  • [TG开发]简单的回声机器人
  • 科技赋能虚拟形象:3D人脸扫描设备的应用与未来
  • vscode+phpstudy+xdebug如何调试php
  • 【R语言】R语言的工作空间映像(workspace image,通常是.RData)详解
  • YOLO v1 输出结构、预测逻辑与局限性详解
  • 教育元宇宙:一场重构教育生态的数字革命
  • 在实验室连接地下车库工控机及其数据采集设备
  • 面向局部遮挡场景的目标检测系统设计与实现
  • 开源WAF新标杆:雷池SafeLine用语义分析重构网站安全边界
  • Go语言实战案例:使用Gin处理路由参数和查询参数
  • .net\c#web、小程序、安卓开发之基于asp.net家用汽车销售管理系统的设计与实现
  • Redis学习——Redis的十大类型String、List、Hash、Set、Zset
  • SQL详细语法教程(一)--数据定义语言(DDL)
  • PCIe Base Specification解析(十)