- YAML是"YAML ain’t Markup language" 不是标记语言的标记语言
- 非常适合做以数据为中心的配置文件
- 基本语法
- key: value : key-value 之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- # 表示注释
- 字符串无需添加引号,如果要添加,"" 双引号不会转义,’'单引号会转义
-
Person.java
@Component//加入到容器中去 @ConfigurationProperties(prefix = "person") @Data @ToString public class Person { private String userName; private Boolean boss; private Date birth; private Integer age; private Pet pet; private String[] interests; private Listanimal; private Map score; private Set salarys; private Map > allPets; } -
Pet.java
@Data @ToString public class Pet { private String name; private Double weight; } -
application.yaml示例
person: userName: 张三 boss: true birth: 2020/10/31 age: 1 pet: name: 花花 weight: 37 interests: - 篮球 - 游泳 animal: [jerry,tom] score: english: 78 math: 79 salarys: - 9999 - 8888 allPets: sick: - {name: tom,weight: 34} - {name: lisi,weight: 56} # - pet: # name: 阿猫 # weight: 23 错误的 health: [{name: jerry,weight: 55}]
-
只需要在pom.xml文件中配置以下内容即可
org.springframework.boot spring-boot-configuration-processor true org.apache.maven.plugins maven-compiler-plugin none



