1. YAML
1.1 YAML是什么
YAML(YAML Ain’t Markup Language):是一种以数据为中心的标记语言,专门用来书写配置文件。
使用YAML需要用到SnakeYAML 依赖,Spring Boot 中的 spring-boot-starter-web 或 spring-boot-starter 都对 SnakeYAML 库做了集成,所以当我们使用了这些库时,Spring Boot 会自动添加 SnakeYAML 库到 classpath 下。
1.2 YAML语法
YAML的语法要求十分严格,我们先看一个修改端口号的示例:
sever:
post: 8080
YAML的语法要求如下:
- 以缩进来控制层级关系
- 空格绝对不能省略(不能为Tab)
- 属性和值的大小写都十分敏感
- 字符串默认不能加上双引号
1.3 YAML常用写法
YAML支持三种数据结构(源自:C语言中文网):
- 对象:键值对的集合
- 数组:一组按次序排列的值
- 字面量:单个的、不可差分的值
2. YAML用法
利用YAML可以给对象的数据注入。
使用YAML时,可以将原来的application.properties删去,然后在同级目录下新建以.yml
为后缀的文件。
2.1 注解注入
2.1.1 POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private int age;
}
2.1.2 添加注解
利用注解注入属性的值,需要用@Component
注解将该实体类注入到Spring容器中。
通过再属性上添加@Value
注解来实现数据注入。
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Student {
@Value("张三")
private String name;
@Value("17")
private int age;
}
2.1.2 测试
在test下有一个xxxxApplicationTests
类,我们在这之中进行(单元)测试。
说明:
- 在测试类中定义私有属性;
- 通过
@Autowired
实现属性的自动装配。
@SpringBootTest
class SpringBootProjectApplicationTests {
@Autowired
private Student student;
@Test
void contextLoads() {
System.out.println(student);
}
}
🔵 输出:Student(name=张三, age=17)
2.2 YAML配置
2.2.1 POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
private Boolean wife;
private Date date;
private Map<String,Object> map;
private List<String> list;
private ArrayList<String> arrayList;
private Student student;
}
2.2.2 YML注入
在application.yml中对实体类person的每个属性进行数据注入。
person:
name: 张三
age: 17
wife: false
date: 2021/10/21
map: {K1: v1, K2: v2, K3: v3}
list:
- L1
- L2
- L3
arrayList:
- AL1
- AL2
- AL3
student:
name: ${person.name:富贵}
age: 17
2.2.3 添加注解
在实体类上添加@Component
、ConfigurationProperties
注解。
ConfigurationProperties作用: 将配置文件中前缀为person
的每一个属性的值,映射到该组件中
@Component
@ConfigurationProperties(prefix = "person")
public class Person {...}
2.3.4 测试
@SpringBootTest
class SpringBootProjectApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
🔵 输出:Person(name=张三, age=17, wife=false, date=Thu Oct 21 00:00:00 CST 2021, map={K1=v1, K2=v2, K3=v3}, list=[L1, L2, L3], arrayList=[AL1, AL2, AL3], student=Student(name=张三, age=17))
2.3 properties配置
除了使用注解、YAML外,可以直接将需要定义的属性与值写在xxx.properties
文件中。
2.3.1 properties文件
该文件以键值对的方式来书写,不要留空格、也不需要加引号。
name=admin
2.3.2 添加注解
由于这是我们自己定义的配置文件,所以我们需要通过@PropertySource
为实体类指定到该配置文件。在上面我们只对一个属性的值进行了定义,所以还需利用EL表达式将键值对的键注入到对应的属性上。
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@PropertySource("classpath:person.properties")
public class Person {
@Value("${name}")
private String name;
private Integer age;
private Boolean wife;
private Date date;
private Map<String,Object> map;
private List<String> list;
private ArrayList<String> arrayList;
private Student student;
}
2.3.3 测试
@SpringBootTest
class SpringBootProjectApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
🔵 输出:Person(name=admin, age=null, wife=null, date=null, map=null, list=null, arrayList=null, student=null)
2.4 YAML/Properties
YAML/Properties的区别:
-
使用YAML配置属性时需要用@ConfigurationProperties注解来绑定到对应的键。
-
使用properties配置属性时需要用@PropertySource注解来“绑定”对应的配置文件。
-
当配置文件同时存在时,
application.properties
的优先级比application.yml
高。
3. 写在最后
配置YAML和Properties实现的是相同的功能,但我们应当推荐选择YAML!