SpringBoot超詳細講解yaml配置文件
1.文件類型
A.properties配置文件類型
同以前properties用法一樣
B.yaml
簡介:
YAML 是 "YAML Ain't Markup Language"(YAML 不是一種標記語言)的遞歸縮寫。在開發(fā)的這種語言時,YAML 的意思其實是:"Yet Another Markup Language"(仍是一種標記語言)。
非常適合用來做以數(shù)據(jù)為中心的配置文件
基本語法
- key :value ;鍵值對象之間必須有一個空格
- 大小寫敏感
- 使用縮進表示層級關(guān)系
- 縮進不允許使用tabl,只允許空格
- 縮進的空格數(shù)不重要,只要相同層級元素左對齊即可
- #表示注釋
- 字符串無需要加引號,如果要加''或""字符串內(nèi)容會被轉(zhuǎn)義或不轉(zhuǎn)義
注意是:字符串不需要加引號,如果加了''單引號或""雙引號內(nèi)容會被轉(zhuǎn)義【單引號轉(zhuǎn)義】或不轉(zhuǎn)義【雙引號不轉(zhuǎn)義】
數(shù)據(jù)類型
A.字面量:
單個的,不可再分的值。date boolean string number null
K: V #鍵值對之間必須有一個空格
B.對象 鍵值對的集合
map Object hash
#行內(nèi)寫法:
K: {k1:v1,k2:v2,k3:v3}
#或者
K:
K1: v1 #鍵值對之間必須有一個空格
k2: v2
k3: v3
C.數(shù)組:一組按次排列的值。
array list set queue
#行內(nèi)寫法
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: ???br /> boss: true
birth: 2000/11/04
age: 21
#對象 鍵值對
pet:
name: 阿狗
weight: 20.28
#數(shù)組
# interests: [聽歌,打代碼,跑步] #行內(nèi)寫法
interests:
- 聽歌
- 打代碼
- 跑步
#List 集合【和數(shù)組寫法一樣】
# animal: [阿貍,阿貓,阿狗] #行內(nèi)寫法
animal:
- 阿貍
- 阿狗
- 阿貓
#set集合【和數(shù)組寫法一樣】
# salary: [8888.8,9999.9,28168.88] #行內(nèi)寫法
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: 阿聯(lián),weight: 20},{name: 阿哈,weight: 21}]
controller控制器
@RestController// 表示該類是一個控制器并且只響應(yīng)瀏覽器不進行頁面跳轉(zhuǎn) public class HelloController { @Autowired Person person; @RequestMapping("/person") public Person person(){ System.out.println(person); return person; } }
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
例如:
<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>
到此這篇關(guān)于SpringBoot超詳細講解yaml配置文件的文章就介紹到這了,更多相關(guān)SpringBoot 配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?8函數(shù)式接口之BinaryOperator使用示例詳解
這篇文章主要大家介紹了Java?8函數(shù)式接口之BinaryOperator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07String s = new String(''a '') 到底產(chǎn)生幾個對象
這篇文章主要介紹了String s = new String(" a ") 到底產(chǎn)生幾個對象,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05mybatis/mybatis-plus模糊查詢語句特殊字符轉(zhuǎn)義攔截器的實現(xiàn)
在開發(fā)中,我們通常會遇到這樣的情況。用戶在錄入信息是錄入了‘%’,而在查詢時無法精確匹配‘%’。究其原因,‘%’是MySQL的關(guān)鍵字,如果我們想要精確匹配‘%’,那么需要對其進行轉(zhuǎn)義,本文就詳細的介紹一下2021-11-11Java使用wait/notify實現(xiàn)線程間通信下篇
wait()和notify()是直接隸屬于Object類,也就是說所有對象都擁有這一對方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12springsecurity第三方授權(quán)認證的項目實踐
Spring security 是一個強大的和高度可定制的身份驗證和訪問控制框架,本文主要介紹了springsecurity第三方授權(quán)認證的項目實踐,具有一定的參考價值,感興趣可以了解一下2023-08-08Intellij IDEA遠程debug教程實戰(zhàn)和要點總結(jié)(推薦)
這篇文章主要介紹了Intellij IDEA遠程debug教程實戰(zhàn)和要點總結(jié)(推薦),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03