yaml非常适合用来做以数据为中心的配置文件
YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
基本语法
- key: value;kv之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义
测试
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.boot</groupId>
<artifactId>boot-01-helloword-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-01-helloword-2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--可以使用注解配置get、set、全参、无参、toString等-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--想要yaml中有提示则需要注释处理器 (注释处理器跟我们业务没有关系只是为了开发方便所以需要在打包插件里面添加配置)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--让springboot重新打包的时候不要把注释处理器打包到jar里面-->
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Person类
package com.atguigu.boot.bean;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author LunarYouI
* @create 2021-04-07 19:16
*/
//把这个类交给Spring管理; 持久层、业务层和控制层中,分别采用@Repository、@Service和@Controller对分层中的类进行凝视,而用@Component对那些比较中立的类进行凝视。
@Component
@ConfigurationProperties(prefix = "person")
@Data
@ToString
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
Pet类
package com.atguigu.boot.bean;
import lombok.Data;
import lombok.ToString;
/**
* @author LunarYouI
* @create 2021-04-07 19:17
*/
@Data
@ToString
public class Pet {
private String name;
private Double weight;
}
yaml配置文件(application.properties文件的优先级要大于application.yaml)
# yaml表示以上对象
person:
# userName: zhangsan
boss: true
birth: 2019/12/12 20:12:33
age: 18
pet:
name: tomcat
weight: 23.4
# interests: [篮球,游泳] 行类写法
interests:
- 篮球
- 足球
- 乒乓球
animal: [阿猫,阿狗]
# animal:
# - jerry
# - mario
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128,second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: 大黄,weight: 99.99}
- name: 二黄
weight: 88.88
- name: 三黄
weight: 77.77
health: [{name: 四黄,weight: 47},{name: 五黄,weight: 47}]
# 因为Person类中的属性是userName,而在yaml中“-n”代表n大写
user-name: 张桑
Controller
package com.atguigu.boot.controller;
import com.atguigu.boot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author LunarYouI
* @create 2021-04-07 19:45
*/
@RestController
public class HelloController {
@Autowired
Person person;
@RequestMapping("/person")
public Person person(){
return person;
}
}