基本语法
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
数据类型
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量:单个的、不可再分的值
yaml在线转json:http://www.bejson.com/validators/yaml/
参考教程:https://www.runoob.com/w3cnote/yaml-intro.html
对象
Person:
id: 1
name: Lisa
teacher:
name: Mr.Lee
age: 40
对应json:
{
Person: {
id: 1,
name: 'Lisa',
teacher: {
name: 'Mr.Lee',
age: 40
}
}
}
另一种写法:
Person: {name: Lisa,age: 18}
对应的json
{
Person: {
name: 'Lisa',
age: 18
}
}
数组
简单数组:
students:
- stu1
- stu2
- stu3
teachers: [tea1,tea2,tea3]
对应json
{
students: [
'stu1',
'stu2',
'stu3'
],
teachers: [
'tea1',
'tea2',
'tea3'
]
}
对象数组:
Teacher:
- id: 1
name: stu1
age: 18
- id: 2
name: stu2
age: 19
对应json
{
Teacher: [
{
id: 1,
name: 'stu1',
age: 18
},
{
id: 2,
name: 'stu2',
age: 19
}
]
}
通过application.yml
文件给SpringBoot属性赋值
新建项目,导入依赖
其中lombok用于简化pojo实体类的编写,spring-boot-starter-test用于测试
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
配置application.yml
person:
id: 1
name: Lisa
happy: true
weight: 93.2
info: {school: qust,grade: 100}
students:
- id: 100
name: LiJing
- id: 200
name: XiaoMing
编写实体类
注意,要将其注入的Spring的容器中
package com.qianyu.pojo;
import lombok.*;
import org.springframework.stereotype.*;
@Data
@Component
public class Student {
private Integer id;
private String name;
}
package com.qianyu.pojo;
import lombok.*;
import org.springframework.boot.context.properties.*;
import org.springframework.stereotype.*;
import java.util.*;
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer id;
private String name;
private boolean happy;
private Double weight;
private Map<String,Object> info;
private List<Student> students;
}
@ConfigurationProperties注解可以将配置文件中的每一个属性的值,映射到指定组件中。即告诉SpringBoot将指定类中所有属性和配置文件中的属性进行绑定
编写主类、测试类
主类:
package com.qianyu;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类:
package com.qianyu;
import com.qianyu.pojo.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.junit4.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Autowired
private Person person;
@Test
public void test() {
System.out.println(person);
}
}
运行结果:
Person(id=1, name=Lisa, happy=true, weight=93.2, info={school=qust, grade=100}, students=[Student(id=100, name=LiJing), Student(id=200, name=XiaoMing)])
说明属性注入成功!
拓展
*.properties
文件配置属性
如果要使用*.properties
类型文件注入的话,需要在类上添加@PropertySource(value="classpath:XXX.properties")
指定配置文件路径,还要在类的属性上使用@Value()
注解才能注入
不同位置application.yml文件的优先级
- file:./config/
- file:./
- classpath:/config/
- classpath:/
SpringBoot多环境
实现方式一:
- 新建
application-dev.yml
、application-pro.yml
两个文件,分别表示开发环境和测试环境 - 在
application.yml
中指定环境
spring:
profiles:
active: dev
实现方式二:
将所有环境写在同一个application.yml
中使用---
分割
spring:
profiles:
active: dev
---
spring:
profiles: dev
server:
port: 8081
---
spring:
profiles: pro
server:
port: 8082