在项目中遇到使用List<List>形式属性配置的问题,记录一下。
一 、使用yml文件配置属性,而非properties文件
environments:
dev:
url: https://dev.example.com
name: Developer Setup
prod:
url: https://another.example.com
name: My Cool App
上面等价下面的properties配置,实际上是会转换成如下的配置:
environments.dev.url=https://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=https://another.example.com
environments.prod.name=My Cool App
二、List 类型的属性配置
my:
servers:
- dev.example.com
- another.example.com
等价如下的properties 配置
my.servers[0]=dev.example.com
my.servers[1]=another.example.com
三、Java 中定义对应配置类
注意要有set方法。
@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
public void setServers(List servers) {
this.servers = servers;
}
}
四、如何定义List<List<String>> 的属性
my:
clusters:
cluster
- c1_Host1,c1_Host2
- c2_Host1,c2_Host2