springboot的yaml写法之map、list和map<String,List>

分两步:设置yaml,注入参数

1.设置yaml,文件名:application.yml

runcommand:
  firstMapList:
    key11:
      - k1
      - k2
      - k3
    key22:
      - k1
      - k2
      - k3
  list:
    - lk1
    - lk2
    - lk3
  maps: {key1: 'value1', key2: 'value2'}

2.代码注入参数

@Component
@Configuration
@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8")
@ConfigurationProperties(prefix = "runcommand")
public class StartCommandConfig {

    private List<String> list;

    private Map<String, String> maps;

    private Map<String, List<String>> firstMapList;

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Map<String, String> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public Map<String, List<String>> getFirstMapList() {
        return firstMapList;
    }

    public void setFirstMapList(Map<String, List<String>> firstMapList) {
        this.firstMapList = firstMapList;
    }
}

3.输出结果

System.out.println(startCommandConfig.getFirstMapList());
System.out.println(startCommandConfig.getList());
System.out.println(startCommandConfig.getMaps());

{key11=[k1, k2, k3], key22=[k1, k2, k3]}
[lk1, lk2, lk3]
{key1=value1, key2=value2}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值