springBoot配置文件properties和yml中数组写法

摘要:这里介绍一下springBoot中的两种文件配置方式中数组的使用,也就是集合。

以下是我springBoot中使用的 application.properties 文件

其实很好理解,我的configs是一个集合,configs[0].appid代表我配置的第一个对象中的appid的值
miniapp.configs[0].appid = 111111
miniapp.configs[0].secret= 222222
miniapp.configs[0].token = 333333
miniapp.configs[0].aesKey = 444444
miniapp.configs[0].msgDataFormat = JSON

miniapp.configs[1].appid = 111
miniapp.configs[1].secret = 222
miniapp.configs[1].token = 333
miniapp.configs[1].aesKey = 444
miniapp.configs[1].msgDataFormat = JSON

这个是使用application.yml的方式,因为YAML 本身支持 list 类型,所以可以在 application.yml 文件中添加:

yml如果配置普通字符串
miniapp:
    configs:
        - appid: 111
          secret: 222
          token: 333
          aesKey: 444
          msgDataFormat: JSON
        - appid: 111
          secret: 222
          token: 333
          aesKey: 444
          msgDataFormat: JSON

这两种方法你选择哪种都可以

下面展示类代码的写法:

解释:

@Data就是省略了get/set方法你可以直接删掉写成get/set
@ConfigurationProperties(prefix = "miniapp"),prefix 这个前缀一定要写对

configs是集合的名字,要和配置表中的信息一致。这样基本就可以了。

数组和map等写法参考:

经验分享 程序员 微信小程序 职场和发展