• SpringBoot的yaml语法


    配置文件

    1、文件类型

    1.1、properties

    同以前的properties用法

    1.2、yaml

    1.2.1、简介

    YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。

    非常适合用来做以数据为中心的配置文件

    1.2.2、基本语法

    • key: value;kv之间有空格
    • 大小写敏感
    • 使用缩进表示层级关系
    • 缩进不允许使用tab,只允许空格
    • 缩进的空格数不重要,只要相同层级的元素左对齐即可
    • '#'表示注释
    • 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义

    1.2.3、数据类型

    • 字面量:单个的、不可再分的值。date、boolean、string、number、null
    k: v
    
    • 对象:键值对的集合。map、hash、set、object
    行内写法:  k: {k1:v1,k2:v2,k3:v3}
    #或
    k: 
      k1: v1
      k2: v2
      k3: v3
    
    • 数组:一组按次序排列的值。array、list、queue
    行内写法:  k: [v1,v2,v3]
    #或者
    k:
     - v1
     - v2
     - v3
    

    1.2.4、示例

    /**
     * @author WGR
     * @create 2021/3/9 -- 20:08
     */
    @Data
    @Component
    @ConfigurationProperties(prefix="person")
    public class Person {
        private String userName;
        private Boolean boss;
        private Date birth;
        private Integer age;
        private Pet pet;
        private String[] interests;
        private List<String> animal;
        private Map<String, Object> score;
        private Set<Double> salarys;
        private Map<String, List<Pet>> allPets;
        private Map<String, List<Pet>> allPets2;
        private List<Pet> pets;
        private List<Map<String, Object>> scores;
    }
    
    /**
     * @author WGR
     * @create 2021/3/9 -- 20:08
     */
    @Data
    public class Pet {
        private String name;
        private Double weight;
    }
    
    

    配置文件:

    person:
      userName: dalianpai
      boss: false
      birth: 2021/03/09 20:12:33
      age: 18
      pet:
        name: huang
        weight: 11.11
    
      interests:
        - qwe
        - asd
      animal: [q,w,e,r]
    #  score: { english: 80, math: 70}
      score:
        english:
          first: 30
          second: 40
          third: 50
        math: [131,140,148]
      salarys: [3999,4999.98,5999.99]
      allPets: {zhangsan: [{name: dahuang, weight: 12.11},{name: xiaogou, weight: 9.11}], wangsi: [{name: wqwq, weight: 12.11},{name: ewqw, weight: 9.11}]}
      allPets2:
        ws: [{name: mario,weight: 47}]
        zs:
          - name: tom
          - name: james
            weight: 32
          - {name: yamy,weight: 12}
      pets:
        - name: qqq
          weight: 123
        - name: qwq
          weight: 323
      scores:
        - k1: v1
        - k2: v2
        - {k3: v3,k4: v3}
    

    测试类:

        @Autowired
        Person person;
    
        @RequestMapping("/person")
        public Person person(){
    
            String userName = person.getUserName();
            System.out.println(userName);
            return person;
        }
    
    {
        "userName": "dalianpai",
        "boss": false,
        "birth": "2021-03-09T12:12:33.000+00:00",
        "age": 18,
        "pet": {
            "name": "huang",
            "weight": 11.11
        },
        "interests": [
            "qwe",
            "asd"
        ],
        "animal": [
            "q",
            "w",
            "e",
            "r"
        ],
        "score": {
            "english": {
                "first": 30,
                "second": 40,
                "third": 50
            },
            "math": {
                "0": 131,
                "1": 140,
                "2": 148
            }
        },
        "salarys": [
            3999,
            4999.98,
            5999.99
        ],
        "allPets": {
            "zhangsan": [
                {
                    "name": "dahuang",
                    "weight": 12.11
                },
                {
                    "name": "xiaogou",
                    "weight": 9.11
                }
            ],
            "wangsi": [
                {
                    "name": "wqwq",
                    "weight": 12.11
                },
                {
                    "name": "ewqw",
                    "weight": 9.11
                }
            ]
        },
        "allPets2": {
            "ws": [
                {
                    "name": "mario",
                    "weight": 47
                }
            ],
            "zs": [
                {
                    "name": "tom",
                    "weight": null
                },
                {
                    "name": "james",
                    "weight": 32
                },
                {
                    "name": "yamy",
                    "weight": 12
                }
            ]
        },
        "pets": [
            {
                "name": "qqq",
                "weight": 123
            },
            {
                "name": "qwq",
                "weight": 323
            }
        ],
        "scores": [
            {
                "k1": "v1"
            },
            {
                "k2": "v2"
            },
            {
                "k3": "v3",
                "k4": "v3"
            }
        ]
    }
    
  • 相关阅读:
    在响应式项目中连接设计与开发
    社交APP的痛点及九大流派解析,微信陌陌,咱还约吗
    网传奶茶妹将进入红杉资本
    OLE、OCX和ActiveX控件之间的比较
    Unity3D网络游戏实战(第2版)
    离婚?在Facebook上把已婚状态改为单身就可以!
    同样酷炫但却失败了的谷歌眼镜,能否给Apple Watch一些前车之鉴?
    Apple Watch首批评测放出:有吐槽、有体贴……毒哭了,暖哭了
    如何培养战略领导力,赢得“长久游戏”?
    博客社交已死,数据社交“永生”
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14508034.html
Copyright © 2020-2023  润新知