在項目中遇到使用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