SpringBoot系列:学习Yaml的使用

作者平台:

| CSDN:blog.csdn.net/qq_41153943

| 掘金:juejin.cn/user/651387…

| 知乎:www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

| 微信公众号:1024笔记

本文大约3279字,预计阅读时长9分钟

前言

SpringBoot的配置文件除了支持application.properties文件格式还支持application.yaml格式的配置文件。

YAML (YAML Aint Markup Language)是一种标记语言,通常以.yml为后缀的文件,是一种直观的能够被电脑识别的数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,比json、xml和等更适合做配置文件。

Yaml语法结构

Yaml基本语法如下:

1、使用缩进表示层级关系

2、大小写敏感

3、缩进不允许使用tab,只允许空格

4、缩进的空格数不重要,只要相同层级的元素左对齐即可

5、字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义

6、key: value;kv之间有空格

Yaml数据结构

Yaml文件支持三种数据结构:

1、字面量:普通的值(数字,字符串,布尔)

key: value:字面直接来写;

字符串默认不用加上单引号或者双引号;

“”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

name: “李青 \n 亚索”:输出:李青 亚索

‘’:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据

name: ‘李青 \n 亚索’:输出:李青 \n 亚索

2、对象:key-value键值对的集合

key: value: In the next line to write the relationship between the object's attributes and values, indentation is required

The object is still key: value

friends:         
    lastName: zhangsan         
    age: 20
复制代码

Inline writing:

friends: {lastName: zhangsan,age: 18}
复制代码

3. Array: a set of values ​​arranged in order (List, Set):

Use - value to represent an element in the array

pets:  
    - cat  
    - dog  
    - pig
复制代码

Array inline writing:

pets: [cat,dog,pig]
复制代码

example

1. Declare a person entity class and Phone entity class in the SpringBoot project:

@Data
@ToString
@Component
//读取配置文件中以person开头的配置
@ConfigurationProperties(prefix = "person")
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Phone phone;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Phone>> allPhones;
}
复制代码

@Data / Automatically add getter and setter methods, need to introduce lombok dependency /
@ToString
public class Phone { private String band; private Double price; } Copy code




2、在pom文件中引入spring-boot-configuration-processor依赖,这样自定义的类和配置文件就可以和默认配置一样可以绑定提示。

org.springframework.boot spring-boot-configuration-processor true Copy code```

3. The above dependencies are only for the convenience of development and are suitable for the development environment, so you can not package them when packaging them, and add the following configuration to the build node of the pom file. You can not add them. It has no effect, but it will affect the size and size of the package. speed:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
复制代码

4. Then write the configuration of Person in yaml format in the application.yml file:

person:
#  单引号会将\n作为字符串输出,双引号会将\n换行输出:所以双引号不会进行转义,而单引号会转义,因为\n本来就是转义换行的意思
#  -n表示N大写,等同于userName
  user-name: 张三
  boss: true
#  默认时间格式是/
  birth: 2020/12/29
  age: 18
  phone:
    band: 华为
    price: 6000.00
#  interests: [篮球,足球]等同于下面格式
#    -代表一个元素
  interests:
    - 篮球
    - 足球
    - 18
    - 175158455
  animal: [猫,狗]
#  score: {english:80,math:90}
  score:
    english: 80
    math: 90
  salarys:
    - 9999.98
    - 9999.99
  allPhones:
    china:
      - {band: 华为,price: 6000.00}
      - band: 小米
        price: 5000.98
      - band: oppo
        price: 4000.36
    America: [{band: Iphone,price: 7000.23},{band: nokia,price: 4002.36}]

复制代码

5. Write a controller class for testing:

@RestController
public class YamlController {
    @Autowired
    private Person person;
    
    @RequestMapping("/person")
    public Person returnPerson(){
        return  person;
    }
}
复制代码

Start the program, enter in the address bar of the browser: http://localhost:8081/share/person, and output the following results:

picture

Summarize

The above is the simple learning of yaml. The yaml format file is widely used in development. It is more intuitive and clear than the properties file, and it is especially suitable for data-centric configuration files. In the configuration of springboot, if the application.properties file and the application.yml file exist at the same time, they will take effect. If the same configuration is set in the two files, the properties file will prevail. application.yml can also be written as application.yaml.

If you have any questions or mistakes, welcome to communicate, discuss and learn together!

The source code of this project is at:

github.com/JiangXia-10…

Welcome to download, Star!

related suggestion:

Guess you like

Origin blog.csdn.net/qq_41153943/article/details/124860706