我试过用
@Value("#{'${white.listed.hotel.ids}'.split(',')}")
private Set<String> fareAlertwhiteListedHotelIds;
但是当 white.listed.hotel.ids 为空时,则也设置为具有空白值的一种尺寸。
white.listed.hotel.ids =
谁能帮我提供一个版本,其中 whiteListedHotelIds 可以包含属性文件中指定的值或没有空白项的项目。
最佳答案
您可以调用自定义方法( as described in this other answer to build a map ,其本身受 @FedericoPeraltaSchaffner's answer 启发):
@Value("#{PropertySplitter.toSet('${white.listed.hotel.ids}')}")
private Set<String> fareAlertwhiteListedHotelIds;
...
@Component("PropertySplitter")
public class PropertySplitter {
public Set<String> toSet(String property) {
Set<String> set = new HashSet<String>();
//not sure if you should trim() or not, you decide.
if(!property.trim().isEmpty()){
Collections.addAll(set, property.split(","));
}
return set;
}
}
在此方法中,您可以随意处理属性(例如,当为空时的特定逻辑)。
关于java - 如何在 Spring Boot 中将属性文件值读入字符串集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204070/