SpringBoot案例-配置文件-yml配置文件

配置格式

  • SpringBoot提供了多种属性配置方式
    • application.properties
    • application.yml
    • application.yaml
  • 常见配置文件格式对比
    • XML(臃肿)
      • <configuration>
          <database>
            <host>localhost</host>
            <port>3306</port>
            <username>admin</username>
            <password>password123</password>
          </database>
        </configuration>
        
    • properties(层级结构不够清晰)
      • database.host=localhost
        database.port=3306
        database.username=admin
        database.password=password123
        
    • yml/yaml(简洁,以数据为中心,推荐使用
      • database:
          host: localhost
          port: 3306
          username: admin
          password: password123
        

yml

基本语法

  • 大小写敏感
  • 数值前必须有空格,作为分隔符
  • 使用缩进表示层级关系,缩进时,不允许使用tab键,只能用空格(idea中会自动将tab转换为空格)
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • # 表示注解,从这个字符一直到行尾,都会被解析器忽略

yml数据格式

  • 对象/Map集合
    • user
          name:zhangsan
          age:20
          password:123
  • 数组/List/Set集合
    • hooby:
          - java
          - game
          - sport

yml配置

  • 将原来的properties配置文件替换掉

猜你喜欢

转载自blog.csdn.net/weixin_64939936/article/details/132476040