一步一步學SpringBoot2,Spring Boot(2) 配置文件

 2023-10-15 阅读 34 评论 0

摘要:Spring Boot(2) 配置文件 學習視頻:https://www.bilibili.com/video/BV19K4y1L7MT?p=20 1.properties 語法:同以前的properties用法 2.yml 2.1 簡介 AML 是 “YAML Ain’t Markup Language”(YAML 不是一種標記語言)的遞歸縮寫

Spring Boot(2) 配置文件

學習視頻:https://www.bilibili.com/video/BV19K4y1L7MT?p=20

1.properties

語法:同以前的properties用法

2.yml

2.1 簡介

  • AML 是 “YAML Ain’t Markup Language”(YAML 不是一種標記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思其實是:“Yet Another Markup Language”(仍是一種標記語言)。
  • 非常適合用來做以數據為中心的配置文件

2.2 基本語法

  • key: value;kv之間有空格
  • 大小寫敏感
  • 使用縮進表示層級關系
  • 縮進不允許使用tab,只允許空格
  • 縮進的空格數不重要,只要相同層級的元素左對齊即可
  • '#'表示注釋
  • 字符串無需加引號,如果要加,’'與""表示字符串內容 會被 轉義/不轉義;如\n用單引號不會換行,用雙引號會換行

2.3 數據類型

  • 字面量:單個的、不可再分的值。date、boolean、string、number、null

    k: v
    
  • 一步一步學SpringBoot2、對象:鍵值對的集合。map、hash、set、object

    行內寫法:  k: {k1: v1,k2: v2,k3: v3}
    #或
    k: k1: v1k2: v2k3: v3
    
  • 數組:一組按次序排列的值。array、list、queue

    行內寫法:  k: [v1,v2,v3]
    #或者
    k:- v1- v2
    - v3
    

2.4 示例

javabean—Person.class

@ConfigurationProperties(prefix = "person")
@Component
@Getter
@Setter
@ToString(includeFieldNames = false, exclude = {"boss", "animal", "score", "salary","allPets"})
@NoArgsConstructor
@AllArgsConstructor
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;
}

javabean—Pet.class

@Component
@ConfigurationProperties(prefix = "pet")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pet {private String name;private Double weight;
}

SpringBoot入門。配置文件—application.yml

person:username: desertsboss: falsebirth: 2000/01/01 11:11:11age: 18pet:name: tomcatweigth: 33.3interes: [跑步, 羽毛球]animal:- dog- cat- pigscore:English: [80, 90, 100]Math:first: 45second: 55thrid: 65salarys: [8888, 9999, 10000]allPets:health:- {name: tom, weight: 30}- {name: jerry}sick:- {name: trump, weight: 100}

3.配置提示

自定義的類和配置文件綁定一般沒有提示,添加依賴

        <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.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></exclude></excludes></configuration></plugin></plugins></build>

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/137206.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息