3 配置文件

配置文件

文件类型

properties

yaml

简介

YAML是“YML Ain‘t Markup Language”(YAML不是一种标记语言)的递归缩写。YAML的意思其实是:“Yet Another Markup Language”(更是一种标记语言)

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

基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许使用空格
  • 所进的空格数不重要,只要相同层级的元素左对齐即可
  • #表示注释
  • 单引号 与 双引号 表示字符串内容 会被转义 / 不转义

数据类型

  • 字面量:单个的,不可再分的值.date/boolean/string/sumber/null

    k: v
    
  • 对象:键值对的集合.map/hash/set/object

    #行内写法:
    k:{k1:v1,k2:v2,k3:v3}
    #或
    k:
      k1:v1
      k2:v2
      k3:v3
    
  • 数组:一组按次序排列的值.array/list/queue

    #行内写法:
    k:[v1,v2,v3]
    #或
    k:
      - v1
      - v2
      - v3
    

示例

Pet类

package com.qin.boot.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pet {
    private String name;
    private Double width;
}

People类

package com.qin.boot.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
public class People{
    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;
}

application.yml

person:
  #单属性
  userName: zhangsan /n lisi
  #单引号会将/n输出至控制台  先转义
  #双引号会换行输出  不转义
  boss: true
  birth: 2000/02/24
  age: 21
  #数组
#  interests: [唱,跳,rap,篮球]
  interests:
    - 唱
    - 跳
    - rap
    - 篮球
  #List
  animal: [阿猫,阿狗]
  #Map
#  score: {english:80,math:90}
  score:
    english: 80
    math: 90
  #set
  salarys:
    - 9999
    - 8888
  #对象
  pet:
    name: tom
    width: 20
  #Map
  allPets:
    sick:
      - {name: ta,width: 99}
      - name: tb
        width: 99
    health: [{name: tc,width: 20},{name: td,width: 30}]

Controller

@RestController
public class TestController {

    @Autowired
    private People people;

    @RequestMapping("/t1")
    public People t1(){
        return people;
    }
}

结果

{"userName":"zhangsan","boss":true,"birth":"2000-02-23T16:00:00.000+00:00","age":21,"pet":{"name":"tom","width":20.0},"interests":["唱","跳","rap","篮球"],"animal":["阿猫","阿狗"],"score":{"english":80,"math":90},"salarys":[9999.0,8888.0],"allPets":{"sick":[{"name":"ta","width":99.0},{"name":"tb","width":99.0}],"health":[{"name":"tc","width":20.0},{"name":"td","width":30.0}]}}

配置提示

在yaml文件中写 类属性使,没有自动提示

需要添加依赖

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

由于这个依赖只是为了方便开发,并没有实际应用,在打jar包时用不到,建议加入以下配置,打jar包时 不将这个jar包导入

<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>
上一篇:idea中Lombok无法使用处理方法


下一篇:Lombok