SpringBoot 在 yml 文件中配置双层List 属性的方法

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值