• SpringBoot2.0---------------7、SpringBoot配置文件


    1. Springboot核心功能

    SpringBoot的核心功能如下:
    在这里插入图片描述

    2. 配置文件

    配置文件类型:
    1、properties
    2、yaml
    Springboot兼容以上两种配置文件,yaml非常适合用来做以数据为中心的配置文件

    2.1 yaml基本语法

    1)key: value,键值之间有空格
    2)大小写敏感
    3)使用缩进表示层级关系
    4)缩进不允许使用Tab,只允许空格
    5)缩进的空格数不重要,只要相同层级的元素左对齐即可
    6)‘#’ 表示注释
    7)字符串无需加引号,如果要加,''与""表示字符串内容,会被转义/不转义

    2.2 yaml数据类型

    字面量:单个的、不可再分的值。date、boolean、string、number、null

    k: v
    
    • 1

    对象:键值对的集合。map、hash、set、object 。如下面格式所示,表示对象k中有三个键值对的两种写法

    行内写法:  k: {k1:v1,k2:v2,k3:v3}
    #或
    k: 
      k1: v1
      k2: v2
      k3: v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    数组:一组按次序排列的值。array、list、queue。如下面格式所示,表示数组k中有v1、v2、v3三个值的两种写法

    行内写法:  k: [v1,v2,v3]
    #或者
    k:
     - v1
     - v2
     - v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例:

    1)与properties配置文件的绑定方式:使用@ConfigurationProperties注解,代码如下
    在这里插入图片描述
    在这里插入图片描述


    2)yaml配置文件:先创建application.yml或application.yaml配置文件;将配置类加入容器中(使用@Component),然后绑定配置文件@ConfigurationProperties(prefix = “person”),当配置文件中使用person.xxx是就会进行绑定;配置类person代码如下:

    @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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    配置类Person中使用的Pet类如下:

    @Data
    @ToString
    public class Pet {
        private String name;
        private Double weight;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2)对Person类进行配置,yaml配置文件写法如下:

    person:
      userName: 'zhangsan \n 李四'
      # 单引号会将 \n作为字符串输出  双引号会将 \n 作为换行输出
      # 双引号不会转义,单引号会转义
      boss: true
      birth: 2019/12/9
      age: 18
      #interests: [篮球,足球]
      interests:
        - 篮球
        - 足球
      animal: [,]
      #score: {english:89,math:90}
      score:
        english: 89
        math: 90
      salarys:
        - 9999
        - 7897
      pet:
        name: aaa
        weight: 5.99
      allPets:
        sick:
          - {name: aaa,weight: 5.99}
          - name: bbb
            weight: 6.99
        health: [{name: ccc,weight: 7.88},{name: ddd,weight: 10}]
    
    • 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
    • 26
    • 27
    • 28

    Springboot配置文件优先推荐使用yaml,因为yaml配置文件层次清晰。当yaml和properties配置文件同时使用且都配置了同一个属性时,按照加载的优先级,同一个属性值会使用properties配置文件中的配置,如图:

    在这里插入图片描述
    在这里插入图片描述
    使用配置后的输出结果中userName属性和age属性的属性值为properties中的值,如图:
    在这里插入图片描述


    2.2 配置提示

    自己写的类中会提示SpringBoot 配置的注释处理器没有在类中找到,解决方法参考官方文档 Configuring the Annotation Processor引入对应依赖即可
    在这里插入图片描述

    1)添加配置处理器:

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-configuration-processorartifactId>
        <optional>trueoptional>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    由于使用配置处理器只是方便开发,对于开发的业务并无影响,因此在打包时不要把配置处理器打包进去,配置 spring-boot-maven-plugin 以防止重新打包目标将依赖项添加到 fat jar 中。

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.springframework.bootgroupId>
                                <artifactId>spring-boot-configuration-processorartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    配置打包时不包含配置处理器

  • 相关阅读:
    RV1126 快速启动
    Stable Diffusion — ControlNet 超详细讲解
    app毕业设计开题报告基于Uniapp+SSM实现的Android的网店系统实现的App购物商城电商
    在vue中使用echarts实现飞机航线 水滴图 词云图
    关闭jenkins插件提醒信息
    NSSCTF做题(4)
    monaco-editor 实现SQL编辑器
    亚马逊面对风控,自养号测评时应该怎么做?
    猿创征文|体验新一代分布式数据库—OceanBase
    HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList的底层实现。
  • 原文地址:https://blog.csdn.net/lyy_sss/article/details/126157909