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

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

方法一:使用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.xdnf.cn/news/17851.html

相关文章:

  • 【SpringBoot】统一功能处理
  • PCIe Base Specification解析(十)
  • GNN: 配送路径最短 GNN 类型方案对比
  • 内容索引之word转md工具 - markitdown
  • Java Record 类 — 简化不可变对象的写法
  • JavaWeb(05)
  • transforms的使用 小土堆pytorch记录
  • 15-docker的企业级私有仓库之docker-harbor
  • 三极管的基极为什么需要下拉电阻
  • docker network 与host的区别
  • GO学习记录四——读取excel完成数据库建表
  • USB基础 -- USB2.0设备插入的过程
  • 《算法导论》第 23 章 - 最小生成树
  • 面试实战 问题二十六 JDK 1.8 核心新特性详解
  • Spring 源码学习(十)—— DispatcherServlet
  • 超实用!ToDesk/网易UU/向日葵:远程办公文件协作效率与安全实测
  • OpenJDK 17 源码 安全点轮询的信号处理流程
  • Spring Boot 整合 Thymeleaf 模板引擎:从零开始的完整指南
  • 数据结构初阶(12)排序算法—插入排序(插入、希尔)(动图演示)
  • 基于R语言的现代贝叶斯统计学方法(贝叶斯参数估计、贝叶斯回归、贝叶斯计算实践过程
  • 为什么 sim(3) 中的尺度 s 与旋转 R 相乘,而不是平移 t?
  • CMake笔记:配置(Configure)、生成(Generate)和构建(Build)
  • 猿大师中间件:Chrome网页内嵌PhotoShop微信桌面应用程序
  • php7 太空船运算符
  • opencv:直方图
  • 【车联网kafka】Kafka核心架构与实战经验(第四篇)
  • mapbox进阶,实现精灵图生成和拆分(小图任意大小,不固定),并简单使用
  • Laravel 使用ssh链接远程数据库
  • 第十六届蓝桥杯青少组C++省赛[2025.8.9]第二部分编程题(1 、庆典队列)
  • 【Java基础|第十八篇】面向对象(八)——包装类