SpringBoot超詳細(xì)講解yaml配置文件
1.文件類型
A.properties配置文件類型
同以前properties用法一樣
B.yaml
簡(jiǎn)介:
YAML 是 "YAML Ain't Markup Language"(YAML 不是一種標(biāo)記語(yǔ)言)的遞歸縮寫。在開發(fā)的這種語(yǔ)言時(shí),YAML 的意思其實(shí)是:"Yet Another Markup Language"(仍是一種標(biāo)記語(yǔ)言)。
非常適合用來(lái)做以數(shù)據(jù)為中心的配置文件
基本語(yǔ)法
- key :value ;鍵值對(duì)象之間必須有一個(gè)空格
- 大小寫敏感
- 使用縮進(jìn)表示層級(jí)關(guān)系
- 縮進(jìn)不允許使用tabl,只允許空格
- 縮進(jìn)的空格數(shù)不重要,只要相同層級(jí)元素左對(duì)齊即可
- #表示注釋
- 字符串無(wú)需要加引號(hào),如果要加''或""字符串內(nèi)容會(huì)被轉(zhuǎn)義或不轉(zhuǎn)義
注意是:字符串不需要加引號(hào),如果加了''單引號(hào)或""雙引號(hào)內(nèi)容會(huì)被轉(zhuǎn)義【單引號(hào)轉(zhuǎn)義】或不轉(zhuǎn)義【雙引號(hào)不轉(zhuǎn)義】
數(shù)據(jù)類型
A.字面量:
單個(gè)的,不可再分的值。date boolean string number null
K: V #鍵值對(duì)之間必須有一個(gè)空格
B.對(duì)象 鍵值對(duì)的集合
map Object hash
#行內(nèi)寫法:
K: {k1:v1,k2:v2,k3:v3}
#或者
K:
K1: v1 #鍵值對(duì)之間必須有一個(gè)空格
k2: v2
k3: v3
C.數(shù)組:一組按次排列的值。
array list set queue
#行內(nèi)寫法
K: [v1,v2,v3]
#或者
K:
- v1 # `-`與`value`值一定要有一個(gè)空格
- 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
#對(duì)象 鍵值對(duì)
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// 表示該類是一個(gè)控制器并且只響應(yīng)瀏覽器不進(jìn)行頁(yè)面跳轉(zhuǎn) public class HelloController { @Autowired Person person; @RequestMapping("/person") public Person person(){ System.out.println(person); return person; } }
2.配置提示
由于在核心配置文件中,配置我們自定義配置信息【自定義的類和配置文件綁定】,IDEA沒(méi)有提示
例如:上述示例一樣沒(méi)有提示
配置提示步驟:
步驟1:引入依賴
在pom.xml加入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
步驟2:加入下面插件,排除在打包時(shí),將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:重新運(yùn)行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超詳細(xì)講解yaml配置文件的文章就介紹到這了,更多相關(guān)SpringBoot 配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?8函數(shù)式接口之BinaryOperator使用示例詳解
這篇文章主要大家介紹了Java?8函數(shù)式接口之BinaryOperator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07String s = new String(''a '') 到底產(chǎn)生幾個(gè)對(duì)象
這篇文章主要介紹了String s = new String(" a ") 到底產(chǎn)生幾個(gè)對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05mybatis/mybatis-plus模糊查詢語(yǔ)句特殊字符轉(zhuǎn)義攔截器的實(shí)現(xiàn)
在開發(fā)中,我們通常會(huì)遇到這樣的情況。用戶在錄入信息是錄入了‘%’,而在查詢時(shí)無(wú)法精確匹配‘%’。究其原因,‘%’是MySQL的關(guān)鍵字,如果我們想要精確匹配‘%’,那么需要對(duì)其進(jìn)行轉(zhuǎn)義,本文就詳細(xì)的介紹一下2021-11-11解決Spring?AOP攔截抽象類(父類)中方法失效問(wèn)題
這篇文章主要介紹了解決Spring?AOP攔截抽象類(父類)中方法失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11詳細(xì)了解JAVA NIO之Buffer(緩沖區(qū))
這篇文章主要介紹了JAVA NIO之Buffer(緩沖區(qū))的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的學(xué)習(xí)JAVA NIO,感興趣的朋友可以了解下2020-07-07Java使用wait/notify實(shí)現(xiàn)線程間通信下篇
wait()和notify()是直接隸屬于Object類,也就是說(shuō)所有對(duì)象都擁有這一對(duì)方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實(shí)現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12springsecurity第三方授權(quán)認(rèn)證的項(xiàng)目實(shí)踐
Spring security 是一個(gè)強(qiáng)大的和高度可定制的身份驗(yàn)證和訪問(wèn)控制框架,本文主要介紹了springsecurity第三方授權(quán)認(rèn)證的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣可以了解一下2023-08-08Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦)
這篇文章主要介紹了Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03