• yml语法学习 SpringBoot配置文件自动装配 yml文件有提示读取配置文件


    yaml学习

    yaml语言的文件后缀名为.yml,和 json 一样是配置文件。

    编码规则

    1. 大小写敏感
    2. 使用缩进表示层级关系
    3. 禁止使用tab缩进,只能使用空格键
    4. 缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
    5. 使用#表示注释
    6. 字符串可以不用引号标注
    7. 双引号"":不会转义字符串里面的特殊字符,特殊字符作为本身想表示的意思。
    8. 单引号’':会将字符串里面的特殊字符转义为字符串处理
    9. |:使用|标注的文本内容缩进表示的块,可以保留块中已有的回车换行
    10. +表示保留文字块末尾的换行,-表示删除字符串末尾的换行。
    11. >:使用 > 标注的文本内容缩进表示的块,将块中回车替换为空格,最终连接成一行。
    12. 使用 & 定义数据锚点(即要复制的数据),使用 * 引用锚点数据(即数据的复制目的地)
    13. 使用 iso-8601 标准表示日期date: 2018-01-01t16:59:43.10-05:00

    数据结构

    配置文件十分简洁,与 json 相比,不用频繁的写 {}[],毕竟换行-符号更加简洁,字符串也不需要频繁的加引号(无论是单引号还是双引号)。

    对象

    #application.yml
    animal: dogs
    homeAddress:
        province: 安徽省
        city: 阜阳市
        county: 利辛县
        detail: 东南路1000000000号
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    转换为 json 为:

    {
      "animal": "dogs",
      "homeAddress": {
        "province": "安徽省",
        "city": "阜阳市",
        "county": "利辛县",
        "detail": "东南路1000000000号"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    数组

    #application.yml
    animals:
        - dogs
        - cats
        - pigs
        - ducks
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    转换为 json 为:

    {
      "animals": [
        "dogs",
        "cats",
        "pigs",
        "ducks"
      ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    字符串

    #application.yml
    # 正常情况下字符串不用写引号
    spring:
      datasource:
        username: root
        password: root
    
    # 字符串内有空格或者特殊字符时需要加引号
    spring:
      datasource:
        url: "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    null

    #application.yml
    
    #.yml 中 ~ 表示 null
    data: ~
    
    • 1
    • 2
    • 3
    • 4

    转换为 json 为:

    {
      "data": null
    }
    
    • 1
    • 2
    • 3

    SpringBoot配置yaml

    我们学习了yaml的语法规则,希望在spring boot项目中能够提示读取配置文件。

    引入依赖

    <!--SpringBoot的application配置组件-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    添加注解

    WxConfig.java

    package com.example.demo.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * @author zby
     * @datetime 2022/7/27 17:23
     * @desc 配置微信配置信息
     */
    @ConfigurationProperties(prefix = "wx")
    @Data
    public class WxConfig {
        private String appId;
        private String appKey;
        private String secret;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    DemoApplication .java

    编写WxConfig .java之后,需要在启动程序【DemoApplication .java】中引入该类,否则编辑器会报出错误。

    package com.example.demo;
    
    import com.example.demo.demo.config.WxConfig;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(WxConfig.class)
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    配置文件已有提示

    在这里插入图片描述

    使用配置

    package com.example.demo.demo;
    
    import com.example.demo.demo.config.WxConfig;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class DemoApplicationTests {
    
      @Autowired private WxConfig wxConfig;
    
      @Test
      void contextLoads() {
        System.out.println(wxConfig);
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出结果

    在这里插入图片描述

  • 相关阅读:
    图论篇--代码随想录算法训练营第五十八天打卡|拓扑排序,dijkstra(朴素版),dijkstra(堆优化版)精讲
    Python Error: The INSERT permission was denied on the object ‘employee“
    安装 laravel 遇到的错误和解决方案
    爬虫项目-爬取股吧(东方财富)评论
    多种DNS的详细搭建方案和实现步骤,自建DNS防止DNS污染、DNS劫持,包括基于bind的named、dnsmasq、c-l-a-s-hdns等。
    springMVC拦截器
    python每日学5:python工程(大型项目)的组织架构:包、模块、类、方法
    Large scale evolutionary optimization using cooperative coevolution
    【Git】学习笔记2.0
    ATF启动(六):bl32(OP-TEE)-->bl33 ATF ending
  • 原文地址:https://blog.csdn.net/lvoelife/article/details/126019156