Spring @ConfigurationProperties Yaml语法配置List和Map:List<String>、List<Obj>、List<List<Obj>>、Map<String,String>、Map<String,List<String>>、Map<String,Obj>、Map<String,List<Obj>>

yaml语法

数据结构可以用类似大纲的缩排方式呈现,结构通过缩进来表示,连续的项目通过减号"-"来表示,map结构里面的key/value对用冒号":"来分隔。

例子:

配置类 YmalConfig:

import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Data
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "testymal")
public class YmalConfig {

    private String configName;

    private UserInfo userInfo;

    private List<String> lists;

    private List<UserInfo> listUserInfo;

    private List<List<UserInfo>> listUserInfos;

    private Map<String,String> maps;

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

    private Map<String,UserInfo> mapUser;

    private Map<String,List<UserInfo>> mapUserList;


    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}

用户信息 UserInfo:

import lombok.Data;

@Data
public class UserInfo {

    private Integer userId;

    private String userName;

}

日志输出(这个类是用来 打印配置) LogConfig:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LogConfig implements CommandLineRunner {

    @Autowired
    YmalConfig ymalConfig;

    @Override
    public void run(String... args) throws Exception {
        log.info("ymalConfig 日志打印配置 : {}", ymalConfig);
    }
}

yml配置文件:

testymal:
  # String configName
  configName: 我是ymal配置

  # UserInfo userInfo
  userInfo:
    userId: 123
    userName: 张三

  # List<String> lists
  lists:
    - 1
    - 2
    - 3
    - 4

  # List<UserInfo> listUserInfo
  listUserInfo:
    - userId: 123
      userName: 张三
    - userId: 321
      userName: 李四

  # List<List<UserInfo>> listUserInfos
  listUserInfos:
    -
      - userId: 123
        userName: 张三
      - userId: 321
        userName: 李四
    -
      - userId: 456
        userName: 王五
      - userId: 654
        userName: 赵六

  # Map<String,String> maps
  maps:
    id: 我是id
    name: 我是name
    age: 我是age

  # Map<String,List<String>> mapList
  mapList:
    userIds:
      - 123
      - 321
    ips:
      - 127.0.0.1
      - 127.0.0.2


  # Map<String,UserInfo> mapUser
  mapUser:
    123:
      userId: 123
      userName: 张三
    321:
      userId: 321
      userName: 李四

  # Map<String,List<UserInfo>> mapUserList
  mapUserList:
    A:
      - userId: 123
        userName: 张三
      - userId: 321
        userName: 李四
    B:
      - userId: 456
        userName: 王五
      - userId: 654
        userName: 赵六

控制台日志打印

ymalConfig 日志打印配置 : {"configName":"我是ymal配置","userInfo":{"userId":123,"userName":"张三"},"lists":["1","2","3","4"],"listUserInfo":[{"userId":123,"userName":"张三"},{"userId":321,"userName":"李四"}],"listUserInfos":[[{"userId":123,"userName":"张三"},{"userId":321,"userName":"李四"}],[{"userId":456,"userName":"王五"},{"userId":654,"userName":"赵六"}]],"maps":{"id":"我是id","name":"我是name","age":"我是age"},"mapList":{"userIds":["123","321"],"ips":["127.0.0.1","127.0.0.2"]},"mapUser":{"123":{"userId":123,"userName":"张三"},"321":{"userId":321,"userName":"李四"}},"mapUserList":{"A":[{"userId":123,"userName":"张三"},{"userId":321,"userName":"李四"}],"B":[{"userId":456,"userName":"王五"},{"userId":654,"userName":"赵六"}]}}

把控制台打印的日志 :Json格式字符串,格式化后,效果如下:

{
    "configName": "我是ymal配置",
    "listUserInfo": [
        {
            "userId": 123,
            "userName": "张三"
        },
        {
            "userId": 321,
            "userName": "李四"
        }
    ],
    "listUserInfos": [
        [
            {
                "userId": 123,
                "userName": "张三"
            },
            {
                "userId": 321,
                "userName": "李四"
            }
        ],
        [
            {
                "userId": 456,
                "userName": "王五"
            },
            {
                "userId": 654,
                "userName": "赵六"
            }
        ]
    ],
    "lists": [
        "1",
        "2",
        "3",
        "4"
    ],
    "mapList": {
        "ips": [
            "127.0.0.1",
            "127.0.0.2"
        ],
        "userIds": [
            "123",
            "321"
        ]
    },
    "mapUser": {
        "123": {
            "userId": 123,
            "userName": "张三"
        },
        "321": {
            "userId": 321,
            "userName": "李四"
        }
    },
    "mapUserList": {
        "A": [
            {
                "userId": 123,
                "userName": "张三"
            },
            {
                "userId": 321,
                "userName": "李四"
            }
        ],
        "B": [
            {
                "userId": 456,
                "userName": "王五"
            },
            {
                "userId": 654,
                "userName": "赵六"
            }
        ]
    },
    "maps": {
        "age": "我是age",
        "id": "我是id",
        "name": "我是name"
    },
    "userInfo": {
        "userId": 123,
        "userName": "张三"
    }
}

done.