本例子使用@configurationProperties注解注入maps对象
1.首先yml文件中配置信息
currency:
link:
maps: {USD: https://xxx.oss-ap-southeast-1.aliyuncs.com/reconciliation_sample.pdf,
AED: https://xxx.oss-ap-southeast-1.aliyuncs.com/reconciliation_sample.pdf,
BTC: https://btc.com/,
ETH: https://etherscan.io/,
LTC: https://live.blockcypher.com/ltc/,
BCH: https://bch.btc.com/}
defaults: '#'
2.定义实体类
/**
* @Author: jinx
* @Date: 2019-05-23 16:32
* @Desc:
*/
@Component
@ConfigurationProperties(prefix = "currency.link")
@Data
public class Link {
/**
* currency:downloadLink
*/
private Map<String,String> maps;
private String defaults;
}
3.使用的地方注入直接遍历该对象中的map集合
@Resource
private Link link;
private String getLink(Currency currency) {
for (String s : link.getMaps().keySet()) {
if (s.equals(currency.name)) {
String result = link.getMaps().get(s);
if (StringUtils.isNotBlank(result)) {
return result;
}
}
}
return link.getDefaults();
}