SpringBoot中application.properties文件存在的问题:配置太多后难阅读和修改,层级结构辨识度不高。
YAML
是"YAML Ain’t a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
特点:
层次分明,方便读写,
使用.yaml或.yml作为文件后缀
#1、k: v v之前是空格区分
#2、属性有层级关系,使用下一行,空两个空格
server:
port: 8080
spring:
data:
redis:
host: localhost
port: 6379
Dog
package com.louis.bean;
public class Dog {
private String name;
private String age;
public Dog() {
}
public Dog(String name, String age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
Person
package com.louis.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
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;
}
public Person(String lastName, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.lastName = lastName;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
在resources下新建一个配置文件application.yml,并添加如下的内容。
#复杂对象
person:
lastName: 张三
age: 18
boss: false
birth: 1990/12/12
maps: { k1: v1,k2: 12 }
lists:
# 数组也可以使用["lisi","zhaoliu"]
‐ lisi
‐ zhaoliu
dog:
name: 迪迪
age: 5
package com.louis;
import com.louis.bean.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Boot304Application {
public static void main(String[] args) {
var ioc = SpringApplication.run(Boot304Application.class, args);
Person bean1 = ioc.getBean(Person.class);
System.out.println("person = " + bean1);
}
}
在yaml中我们还可以使用松散绑定这一操作。如:birthDay推荐写为birth-day,可以实现驼峰命名和中划线的转换
|
开头,大文本写在下层,保留文本格式,换行符正确显示
>
开头,大文本写在下层,折叠换行符,如果文本存在缩进则保留原来的格式,没有缩进的情况下压缩格式
。
# 第一个 YAML 文档
name: John
age: 30
---
# 第二个 YAML 文档
name: Alice
age: 25