码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中的@Service("dataDictionaryService")注解

时间:2020-01-08 14:18:10      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:注解   control   mic   try   spring   mamicode   自动   ret   ==   

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)

Spring中的@Service("dataDictionaryService")注解

标签:注解   control   mic   try   spring   mamicode   自动   ret   ==   

原文地址:https://www.cnblogs.com/vole/p/12166225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!