SpringBoot配置文件格式简介

简单说明了yml配置文件的格式

SpringBoot配置文件格式简介

SpringBoot项目提供了多种属性配置方式(properties、yaml、yml)

properties:

1
2
3
4
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/web01
spring.datasource.username=root
spring.datasource.password=1234

yaml/yml(一样的):

1
2
3
4
5
6
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

yml配置文件

格式:

  • 数值前边必须有空格,作为分隔符

  • 使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格(idea中会自动将Tab转换为空格)

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

  • # 表示注释,从这个字符一直到行尾,都会被解析器忽略

定义对象/Map集合:

1
2
3
4
5
user:
  name: 张三
  age: 18
  gender: male
  password: '0123456'

定义数组/List/Set集合:

1
2
3
4
hobby:
  - sleep
  - game
  - smoke

注意:在yml格式的配置文件中,如果配置项的值是以 0 开头的,值需要使用 ’’ 引起来,因为以0开头在yml中表示8进制的数据

本站于2025年3月26日建立
使用 Hugo 构建
主题 StackJimmy 设计