知乎专栏 |
List类型在properties文件中使用[]来定义列表类型,比如:
sms.url[0]=http://api1.example.com sms.url[1]=http://api2.example.com netkiller.book[0].title=Netkiller Linux 手札 netkiller.book[0].author=netkiller netkiller.book[1].title=Netkiller Spring 手札 netkiller.book[1].author=netkiller
注意:在Spring Boot 2.0中对于List类型数组下标的配置必须是连续的,否则会抛出UnboundConfigurationPropertiesException异常,所以如下配置是不允许的:
foo[0]=a foo[2]=b
使用逗号分割的配置方式,上面与下面的配置是等价的:
sms.url[0]=http://api1.example.com,http://api2.example.com
在yaml文件中使用可以使用如下配置:
email: to: address: - neo@netkiller.cn - jam@netkiller.cn
逗号分割的方式:
email: to: address: neo@netkiller.cn, jam@netkiller.cn
命令行传递 List 数据
java -jar -D"api.url[0]=http://api1.example.com" \ -D"api.url[1]=http://api2.example.com" \ api.netkiller.cn-v1.0.jar 逗号分割的方式,比如: java -jar -Dapi.url=http://api1.example.com,http://api2.example.com demo.jar