1、接口

public interface DataDictionaryService {
    /**
     * 获取兑换率
     * @param coinType 原币种
     * @param billCoin 折币种
     * @param date 取离该时间最近的兑换率
     * @return
     */
    public BigDecimal getExch(String coinType, String billCoin, Date date);

}

 如图,有两个实现类。

2、实现类

@Service("dataDictionaryService")
public class DataDictionaryCache implements DataDictionaryService{
    @Autowired
    private DataDictionaryServiceImpl dataDictionaryService;

    private Map<ExchConditions,Exch> exchMap = new ConcurrentHashMap<>();

    @Autowired
    private CacheManager cacheManager;

    //private Map<PrpDcurrency,prpDcurrency> prpDcurrencyMap=new ConcurrentHashMap<>();

    @Override
    public BigDecimal getExch(String coinType, String billCoin, Date date) {
        ExchConditions exchConditions = new ExchConditions(coinType,billCoin,date);
        if(!exchMap.containsKey(exchConditions)
                || exchMap.get(exchConditions).isTimeout()){
            BigDecimal exch = dataDictionaryService.getExch(coinType,billCoin,date);
            exchMap.put(exchConditions,new Exch(exch));
        }
        return exchMap.get(exchConditions).getExchrate();
    }
}


@Component
public class DataDictionaryServiceImpl implements DataDictionaryService {

@Autowired
private DictAPIService dictService;

@Autowired
private RelationBreakMapper relationBreakMapper;

@Autowired
private OutstandingBreakMapper outstandingBreakMapper;
@Autowired
private AssessBreakMapper assessBreakMapper;

/**
* 获取兑换率
* @param coinType 原币种
* @param billCoin 折币种
* @param date 取离该时间最近的兑换率
* @return
*/
@Override
public BigDecimal getExch(String coinType, String billCoin, Date date) {
PrpDexch exch = null;
try {
exch = dictService.getPrpDexch("dms", date, coinType, billCoin);
} catch (Exception e) {

return BigDecimal.ZERO;
}
return exch == null ? BigDecimal.ZERO : exch.getExchRate().divide(new BigDecimal(exch.getBase()));
}

3、调用

@Controller
@RequestMapping(value ="/dataDict")
public class DataDictController {

    @Autowired
    private DataDictionaryService dataDictionaryService;

说明:当一个接口有两个实现类时,需要在一个实现类的注解后加上@Service("dataDictionaryService")引号中的说明,在Controller调用时,属性名称必须与引号中的名称一致,不然无法识别创建哪个实现类的Bean。

其实会出现编译报错如下:

不能自动装配。“DataDictionaryService”类型的bean不止一个。

bean:

dataDictionaryCache (dataDictionaryCache .java)

dataDictionaryServiceImpl (dataDictionaryServiceImpl .java)

12-23 13:34