目录
  • 1.文件类型
    • A.properties配置文件类型
    • B.yaml
      • 基本语法
      • 数据类型
  • 2.配置提示

    1.文件类型

    A.properties配置文件类型

    同以前properties用法一样

    B.yaml

    简介:

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

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

    基本语法

    • key :value ;键值对象之间必须有一个空格
    • 大小写敏感
    • 使用缩进表示层级关系
    • 缩进不允许使用tabl,只允许空格
    • 缩进的空格数不重要,只要相同层级元素左对齐即可
    • #表示注释
    • 字符串无需要加引号,如果要加''或""字符串内容会被转义或不转义

    注意是:字符串不需要加引号,如果加了''单引号或""双引号内容会被转义【单引号转义】或不转义【双引号不转义】

    数据类型

    A.字面量:

    单个的,不可再分的值。date boolean string number null

    K: V  #键值对之间必须有一个空格

    B.对象 键值对的集合

    map Object hash

    #行内写法:
    K: {k1:v1,k2:v2,k3:v3}
    #或者
    K:
        K1: v1  #键值对之间必须有一个空格
        k2: v2
        k3: v3

    C.数组:一组按次排列的值。

    array list set queue

    #行内写法
    K: [v1,v2,v3]

    #或者
    K:
        – v1  # `-`与`value`值一定要有一个空格
        – v2
        – v3

    示例:

    POJO

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    @Component
    public class Pet {
        private String name;
        private Double weight;
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    @Component
    @ConfigurationProperties(prefix = "person")
    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> salary;
        private Map<String,List<Pet>> allPets;
    }
    

    yaml配置文件

    person:
      #字面量
      username: 海康
      boss: true
      birth: 2000/11/04
      age: 21
      #对象 键值对
      pet:
        name: 阿狗
        weight: 20.28
      #数组
    #  interests: [听歌,打代码,跑步] #行内写法
      interests:
        – 听歌
        – 打代码
        – 跑步
      #List 集合【和数组写法一样】
    #  animal: [阿狸,阿猫,阿狗] #行内写法
      animal:
        – 阿狸
        – 阿狗
        – 阿猫
      #set集合【和数组写法一样】
    #  salary: [8888.8,9999.9,28168.88] #行内写法
      salary:
        – 88988.99
        – 978988.9
        – 9999168.98
      #Map<String,Object>集合
      score:
        java: 88.8
        C++: 88.99

      #Map<String,List<Pet>> 集合
      allPets:
        haikang:
          – name: 阿狸
            weight: 20.9
          – name: 阿狗
            weight: 30.2
        iaia: [{name: 阿联,weight: 20},{name: 阿哈,weight: 21}]

    controller控制器

    @RestController// 表示该类是一个控制器并且只响应浏览器不进行页面跳转
    public class HelloController {
        @Autowired
        Person person;
        @RequestMapping("/person")
        public Person person(){
            System.out.println(person);
            return  person;
        }
    }

    SpringBoot超详细讲解yaml配置文件

    2.配置提示

    由于在核心配置文件中,配置我们自定义配置信息【自定义的类和配置文件绑定】,IDEA没有提示

    例如:上述示例一样没有提示

    配置提示步骤:

    步骤1:引入依赖

    在pom.xml加入

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

    步骤2:加入下面插件,排除在打包时,将configuration-processor的引入打包jar

    在pom.xml加入

     <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>
    

    步骤3:重新运行RUN

    SpringBoot超详细讲解yaml配置文件

    SpringBoot超详细讲解yaml配置文件

    例如:

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
        <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>
    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。