如何从application.properties加载二维整数数组至Java类
解决Spring中加载嵌套列表配置的问题
方法1:调整配置格式+SpEL表达式处理
先修改application.properties里的配置,用分号分隔外层嵌套元素(避免和内层逗号冲突):
my.list.of.strings={1,2};{2,3}
然后在Java类中通过SpEL表达式完成嵌套拆分:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List; @Component public class MyComponent { // 加载为字符串类型嵌套列表 @Value("#{${my.list.of.strings}.split(';').stream().map(item -> item.replaceAll('[{}]', '').split(',')).collect(java.util.stream.Collectors.toList())}") private List<List<String>> myStringList; // 若需要Integer类型,增加类型转换步骤 @Value("#{${my.list.of.strings}.split(';').stream().map(item -> item.replaceAll('[{}]', '').split(',')).map(arr -> java.util.Arrays.stream(arr).map(Integer::valueOf).collect(java.util.stream.Collectors.toList())).collect(java.util.stream.Collectors.toList())}") private List<List<Integer>> myIntList; }
原理:先按分号拆分得到每个{1,2}格式的子串,移除大括号后再按逗号拆分内层元素,最终收集为嵌套列表。
方法2:自定义属性转换器(保留原配置格式)
如果不想修改原配置my.list.of.strings={{1,2},{2,3}},可以自定义转换器实现字符串到嵌套列表的转换:
- 编写转换器类:
import org.springframework.core.convert.converter.Converter; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class NestedListConverter implements Converter<String, List<List<Integer>>> { private static final Pattern PATTERN = Pattern.compile("\\{(.*?)}"); @Override public List<List<Integer>> convert(String source) { Matcher matcher = PATTERN.matcher(source); return matcher.results() .map(match -> match.group(1)) .map(innerStr -> Arrays.stream(innerStr.split(",")) .map(Integer::valueOf) .collect(Collectors.toList())) .collect(Collectors.toList()); } }
- 注册转换器到Spring上下文:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; @Configuration public class ConverterConfig { @Bean public Converter<String, List<List<Integer>>> nestedListConverter() { return new NestedListConverter(); } }
- 直接通过
@Value注入:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List; @Component public class MyComponent { @Value("${my.list.of.strings}") private List<List<Integer>> myList; }
方法3:使用@ConfigurationProperties(推荐)
这是Spring Boot推荐的复杂配置绑定方式,结构清晰易维护:
- 编写配置类:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; @Component @ConfigurationProperties(prefix = "my.list") public class MyListProperties { private List<List<Integer>> ofStrings; public List<List<Integer>> getOfStrings() { return ofStrings; } public void setOfStrings(List<List<Integer>> ofStrings) { this.ofStrings = ofStrings; } }
- 配置文件支持两种写法(任选其一):
# 写法1:索引式配置 my.list.of-strings[0][0]=1 my.list.of-strings[0][1]=2 my.list.of-strings[1][0]=2 my.list.of-strings[1][1]=3 # 写法2:简化嵌套格式(和你原配置格式一致) # my.list.of-strings={{1,2},{2,3}}
- 注入配置类使用:
import org.springframework.stereotype.Component; import java.util.List; @Component public class MyComponent { private final MyListProperties myListProperties; public MyComponent(MyListProperties myListProperties) { this.myListProperties = myListProperties; } public void processList() { List<List<Integer>> myList = myListProperties.getOfStrings(); // 业务逻辑处理 } }
内容的提问来源于stack exchange,提问作者Rahul
相关产品推荐
相关产品推荐

