SpringBoot詳細(xì)講解yaml配置文件的用法
1.基本語(yǔ)法
- key: value;kv之間有空格
- 大小寫(xiě)敏感
- 使用縮進(jìn)表示層級(jí)關(guān)系
- 縮進(jìn)不允許使用tab,只允許空格
- 縮進(jìn)的空格數(shù)不重要,只要相同層級(jí)的元素左對(duì)齊即可
- '#'表示注釋
- 字符串無(wú)需加引號(hào),如果要加,單引號(hào)’'、雙引號(hào)""表示字符串內(nèi)容會(huì)被 轉(zhuǎn)義、不轉(zhuǎn)義
2.數(shù)據(jù)類型
1.字面量:?jiǎn)蝹€(gè)的、不可再分的值。date、boolean、string、number、null
k: v
2.對(duì)象:鍵值對(duì)的集合。map、hash、set、object
#行內(nèi)寫(xiě)法:
k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
3.數(shù)組:一組按次序排列的值。array、list、queue
#行內(nèi)寫(xiě)法:
k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
3.代碼測(cè)試
User
package com.limi.springboottest2.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private String userName; private Integer age; private String gender; }
Entity1
package com.limi.springboottest2.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @ConfigurationProperties(prefix = "entity1") @Data @AllArgsConstructor @NoArgsConstructor @Component public class Entity1 { private Double number; private List<String> array; private User user; private Map<String, Integer> map; private String str0; private String str1; private String str2; }
application.yml
entity1:
number: 11
array: ["apple", "peach", "orange"]
user: {userName: "lily", }
map: {"Math": 100,"English": 98,"Art": 8}
#對(duì)比字符串變量不使用引號(hào)、使用單引號(hào)、雙引號(hào)的區(qū)別
str0: \n 666
str1: '\n 666'
str2: "\n 666"
HelloController
package com.limi.springboottest2.controller; import com.limi.springboottest2.entity.Entity1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @Autowired private Entity1 entity1; @GetMapping("/test1") @ResponseBody void test1() { System.out.println(entity1); } }
測(cè)試結(jié)果
可以看到
- 不使用引號(hào)和使用單引號(hào)的字符串: n 666 中的\n是直接輸出\n
- 使用雙引號(hào)的字符串: \n 666 中的\n是輸出為換行符
4.開(kāi)啟補(bǔ)全提示
就是下圖的效果
自定義的類和配置文件綁定一般沒(méi)有提示。若要提示,添加如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- 下面插件作用是工程打包時(shí),不將spring-boot-configuration-processor打進(jìn)包內(nèi),讓其只在編碼的時(shí)候有用 --> <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 yaml配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí)
這篇文章主要為大家詳細(xì)介紹了Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03Spring整合Quartz分布式調(diào)度的示例代碼
本篇文章主要介紹了Spring整合Quartz分布式調(diào)度的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04springcloud如何用Redlock實(shí)現(xiàn)分布式鎖
本文主要介紹了springcloud如何用Redlock實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11mybatis中association和collection的使用與區(qū)別
在 MyBatis 中,<association>?和?<collection>?是用于配置結(jié)果映射中關(guān)聯(lián)關(guān)系的兩個(gè)元素,本文主要介紹了mybatis中<association>和<collection>的使用與區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Java編程中隨機(jī)數(shù)的生成方式總結(jié)
在Java中利用自帶的類庫(kù)可以有三種途徑可以產(chǎn)生隨機(jī)數(shù),這里我們舉了一些簡(jiǎn)單的例子來(lái)進(jìn)行Java編程中隨機(jī)數(shù)的生成方式總結(jié),需要的朋友可以參考下2016-05-05