使用场景
实际项目上中遇到交易使用多种支付方式,如微信原生支付,微信小程序支付,微信公众号支付,支付宝之支付等等等。简化多个if判断,switch case 使用。
使用介绍
1、首先说一下spring Autowired 注解除了我们正常使用的之外还有一点:
@Autowired标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类 型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key。非常牛X的操作。
需要注意的是:应用于map的时候key必须为String类型(实现类的名字)
看下源码
好啦 直接上代码:
cotroller注入
public class x{
@Autowired
private Map<String, PayStrategy> payStategies;
@RequestMapping(value = "/pay",method = RequestMethod.GET)
public Object pay(int storeType, String version, int channel,.....) {
PayStrategy payStrategy = null;
PayChannelStrategyEnum payChannelStrategyEnum = PayChannelStrategyEnum.getPayChannelStrategyEnum(channel);
if (payChannelStrategyEnum == null) {
outMap.put("code", "fail");
outMap.put(