游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • SpringBoot2

    • 各种小知识
    • 一、SpringBoot基础入门
    • 二、SpringBoot的底层注解
    • 三、SpringBoot的自动配置
    • 四、SpringBoot的最佳实践
    • 五、SpringBoot的配置文件
      • 20、配置文件-yaml的用法
        • 基本语法
        • 数据类型
        • 实例
      • 21、配置文件-自定义类绑定的配置提示
    • 六、SpringBoot的Web场景
    • 七、SpringBoot的请求处理(源码分析)
  • JavaWeb

  • SSM

  • SpringBoot3

  • 技术
  • SpringBoot2
yangzhixuan
2023-05-10
目录

五、SpringBoot的配置文件

# 20、配置文件-yaml的用法

同以前的properties用法

YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。

非常适合用来做以数据为中心的配置文件。

# 基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号,如果要加,单引号''、双引号""表示字符串内容分别会被 转义(将\n作为字符串输出)、不转义(将\n作为换行输出,因为\n的原本意义就是换行)
  • 使用-连接单词能够代替驼峰的首字母大写

# 数据类型

  • 字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
1
  • 对象:键值对的集合。map、hash、set、object
#行内写法:  

k: {k1:v1,k2:v2,k3:v3}

#或

k: 
  k1: v1
  k2: v2
  k3: v3
1
2
3
4
5
6
7
8
9
10
  • 数组:一组按次序排列的值。array、list、queue
#行内写法:  

k: [v1,v2,v3]

#或者

k:
 - v1
 - v2
 - v3
1
2
3
4
5
6
7
8
9
10

# 实例

@ConfigurationProperties(prefix = "person")
@Component
@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;
}

@Data
public class Pet {
    private String name;
    private Double weight;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

用yaml表示以上对象

person:
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet: 
    name: tomcat
    weight: 23.4
  interests: [篮球,游泳]
  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: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

配置文件中使用单引号或双引号定义值也能正常生效,例

person:
    userName: 'zhangsan'
    boss: false
    birth: 2019/12/12 20:12:33
    age: '18'
1
2
3
4
5

输出{"userName":"zhangsan","boss":false,"birth":"2019-12-12T12:12:33.000+00:00","age":18}

# 21、配置文件-自定义类绑定的配置提示

You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled.——link (opens new window)

自定义的类和配置文件绑定一般没有提示。若要提示,添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

<!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 -->
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
四、SpringBoot的最佳实践
六、SpringBoot的Web场景

← 四、SpringBoot的最佳实践 六、SpringBoot的Web场景→

最近更新
01
tensor比较大小函数
05-30
02
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
03
半监督学习经典方法 Π-model、Mean Teacher
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式