文章目录
  1. 1. springboot中yml文件
    1. 1.1. yml语法
    2. 1.2. 测试
      1. 1.2.1. 实体配置
      2. 1.2.2. 测试yml配置
      3. 1.2.3. 开启配置
      4. 1.2.4. 读取配置信息并打印

springboot中yml文件

yml语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
k:(空格)v :表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,就是同一层级,几个空格不重要。
属性和值是大小写敏感。
字面量:普通值(数字、字符串、布尔)
字符串默认不用加上单引号或者双引号。
"": 双引号,不会自动转义字符串里面的特殊字符串;特殊字符会作为本身想表示的意思。
name: "zhangsan \n lisi" -》输出:zhangsan 换行 lisi
'': 单引号,会自动转义字符串里面的特殊字符串;下面的 \ 自动转义成了 \\
name: "zhangsan \n lisi" -》输出:zhangsan \n lisi
对象(List、Map、键值对形式)
k:v: 在下一行写对象的属性和值;注意缩进
对象还是 k: v 的方式
friends:
lastName: zhangsan
age: 20
行内写法: friends: {lastName: zhangsan, age: 18}
数组(List、Set)
用 -值表示数组中的元素
pets:
- cat
- dog
- pig
行内写法: pets: [cat,dog,pig];

测试

实体配置

此处省略了get,set,toString方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@ConfigurationProperties(prefix = "com.yml.test")
public class Person {
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
public static class Dog{
private String name;
private Integer age;
}
}

测试yml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
com:
yml:
test:
name: hello
age: 20
boss: true
birth: 1999/05/20
maps: {hello: hello, world: world}
lists:
- 11111
- 22222
dog:
name: tom
age: 2

开启配置

1
2
3
4
5
@Configuration
@EnableConfigurationProperties({Person.class})
配置类{

}

读取配置信息并打印

1
Person{name='hello', age=20, boss=true, birth=Thu May 20 00:00:00 CST 1999, maps={hello=hello, world=world}, lists=[11111, 22222], dog=Dog{name='tom', age=2}}
文章目录
  1. 1. springboot中yml文件
    1. 1.1. yml语法
    2. 1.2. 测试
      1. 1.2.1. 实体配置
      2. 1.2.2. 测试yml配置
      3. 1.2.3. 开启配置
      4. 1.2.4. 读取配置信息并打印