天天看點

4、檔案類型

1、檔案類型

1.1、properties

同以前的properties用法

1.2、yaml

1.2.1、簡介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一種标記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思其實是:“Yet Another Markup Language”(仍是一種标記語言)。

1.2.2、基本文法

  • key: value.;kv之間有空格
  • 大小寫敏感
  • 使用縮進表示層級關系
  • 縮進不允許使用tab,隻允許空格(在IDEA中,tab會自動轉換成空格)
  • 縮進的空格數不重要,隻要相同層級的元素左對齊即可
  • '#'表示注釋
  • 字元串無需加引号,如果要加,’'與""表示字元串内容 會被 轉義/不轉義

1.2.3、資料類型

  • 字面量:單個的、不可再分的值。date、boolean、string、number、null
k:      
  • 對象,Map集合:鍵值對的形式。
行内寫法:  k: {k1:v1,k2:v2,k3:v3}
#或
k: 
  k1: v1
  k2: v2
  k3:      
  • 數組:一組按次序排列的值。array、list、queue
行内寫法:  k: [v1,v2,v3]
#或者
k:
 - v1
 - v2
 -      

1.2.4、示例

@ConfigurationProperties(prefix = "person")
@Component
@ToString
@Data
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;
}

@Data
public class Pet {
    private String name;
    private Double weight;
}      
# 注意:單引号會将\n作為字元串輸出,而雙引号 會将\n作為換行輸出
# 雙引号不會進行轉義,單引号會進行轉義.
person:
  userName: 'zhangsan \n 李四'
  boss: false
  birth: 2021/9/6
  age: 21
#  Pet: {name:tomcat,weight:99.99}
  pet:
    name: tomcat
    weight: 99.99
#  interests: [籃球,足球]
  interests:
    - 籃球
    - 足球
    - 21
  animal: [阿貓,阿狗]
  score: {english: 80,math: 90}
  salarys:
    - 9999.99
    - 9999.85
  allPets:
    sick:
      - {name: 阿狗,weight: 99.99}
      - name: 阿貓
        weight: 88.88
      - name: 阿蟲
        weight: 77.77
    health:
      - {name: 阿狗,weight: 99.99}
      - {name: 阿豬,weight: 99.99}      

2、配置提示

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


 <build>
        <plugins>
            <plugin>
                
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    # 打包時不包括這些小插件.
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>