yaml 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言),用来编写配置文件
以前的配置文件,大多数都是使用xml来配置,比如 db.properties 。
现在 越来越多的人使用yaml语法来编写配置文件
这种语言以数据作为中心,而不是以标记语言为重点
传统xml配置
<server>
<port>8081<port>
server>
yaml配置
server:
prot: 8081
1、空格不能省略
2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。
3、属性和值的大小写都是十分敏感的。
直接写在后面就可以 , 字符串默认不用加上双引号或者单引号
“ ”
双引号,不会转义字符串里面的特殊字符
比如 :name: “csnz \n 潮汕奴仔” 输出 :csnz 换行 潮汕奴仔
''
单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出
比如 :name: ‘csnz \n 潮汕奴仔’ 输出 :csnz \n 潮汕奴仔
形式: 使用换行来表示 对象的属性和值的关系
#对象、Map格式
k:
v1:
v2:
teacher:
name: 三上老师
age: 26
亦可以换成 行内写法 (注意冒号后面都有空格)
teacher: {name: 三上老师,age: 26}
用 - 值表示数组中的一个元素,比如:
pets:
- cat
- dog
- pig
行内写法
pets: [cat,dog,pig]
1、在springboot项目中的resources目录下新建一个文件 application.yml
2、新建两个实体类 dog、person
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Dog {
@Value("潮汕好狗狗")
private String name;
@Value("5")
private Integer age;
public Dog() {
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
public class Person {
private String name;
private Integer age;
private Boolean MarryStatus;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public Person(String name, Integer age, Boolean marryStatus, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
MarryStatus = marryStatus;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getMarryStatus() {
return MarryStatus;
}
public void setMarryStatus(Boolean marryStatus) {
MarryStatus = marryStatus;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", MarryStatus=" + MarryStatus +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
@SpringBootTest
class Springboot03ConfigApplicationTests {
@Autowired
private Dog dog;
@Test
void contextLoads() {
System.out.println(dog);
}
}
person类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
*/
@Component //注册bean
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean MarryStatus;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public Person(String name, Integer age, Boolean marryStatus, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
MarryStatus = marryStatus;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getMarryStatus() {
return MarryStatus;
}
public void setMarryStatus(Boolean marryStatus) {
MarryStatus = marryStatus;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", MarryStatus=" + MarryStatus +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
使用yaml配置的方式进行注入
application.yaml
person:
name: 潮汕奴仔
age: 18
MarryStatus: false
birth: 2021/7/31
maps: {k1: father,k2: mother}
lists:
- girl
- basketball
- CSGO
dog:
name: 潮汕奴仔专属
age: 6