如何在Spring Boot中将属性文件值读入字符串集

问题描述 投票:3回答:3

我尝试使用

@Value("#{'${white.listed.hotel.ids}'.split(',')}")
private Set<String> fareAlertwhiteListedHotelIds;

但是当white.listed.hotel.ids为空时,设置的大小也将为一,空白值。

white.listed.hotel.ids =

有人可以帮我提供一个版本,在该版本中,whiteListedHotelIds可以包含值,如果在属性文件中指定该值,则可以包含空白的项目。

java spring spring-boot java-8 properties-file
3个回答
5
投票

您可以调用自定义方法(as described in this other answer to build a map,其本身受到@FedericoPeraltaSchaffner's answer的启发):


0
投票

通过构造函数注入@Value(如往常一样),并在那里进行所需的所有后处理:


0
投票

您也可以使用spring表达式语言进行验证,如果提供的字符串为空,则返回空数组或将输入字符串拆分为array。在jdk-11中,您可以直接使用isBlank

© www.soinside.com 2019 - 2024. All rights reserved.