springboot之yaml使用

yml与properties会一起生效

1.基本语法

1.key与value之间存在空格

2.大小写敏感

3.使用缩进表示层级关系

4.相同层级左元素对齐

5.#表示注释

6.字符串无需引号,''与""表示字符串内容转义或不转义(如字符串中'\n’等转义字符,''将\n作为字符串输出,""将\n作为换行输出)

2.数据类型

字面量:单个不可分的值,java基本数据类型 写法K: V

Map:键值对集合 写法K:{K1: V1,K2: V2,...} 或

K: 

    K1:V1

    K2:V2

    ....

 

数组:数值集合(Array,queue,list)

写法K: [V1,V2,...]或

K: 

    - V1

    - V2

    ....

3.yml中配置类属性的值

1.定义前缀

@ConfigurationProperties(prefix = "person")
@Component
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private String[] interests;
private Map<String,Object> score;
private Pet pet;
private List<String> petName;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}

2.yml中定义

person:
userName: Lisi
boss: false
birth: 1998/1/1
age: 25
pet:
name: jerryMouse
weight: 3.4
interests: [swim,dance]
petName: [AAI,IEE]
score: {english: 90,math: 80}
salarys: [9999,9998]
allPets:
sick:
- {name: tomcat,weight: 23.3}
- {name: aio,weight: 23.3}
health:
- {name: pom,weight: 23.3}
- {name: rpg,weight: 23.3}
server:
port: 8888

  注意

english: 90之间有空格,凡是键值对之间必须有空格
上一篇:拷贝和替换算法操作指南


下一篇:PAT (Advanced Level) Practice 1048 Find Coins (25 分) 凌宸1642