SpringBoot詳細(xì)講解yaml配置文件的用法
1.基本語(yǔ)法
- key: value;kv之間有空格
- 大小寫敏感
- 使用縮進(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)寫法:
k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
3.數(shù)組:一組按次序排列的值。array、list、queue
#行內(nèi)寫法:
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.開啟補(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)文章
IDEA中調(diào)用方法時(shí),如何同步顯示方法的注釋信息
這篇文章主要介紹了IDEA中調(diào)用方法時(shí),如何同步顯示方法的注釋信息問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例
這篇文章主要介紹了Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04
使用java代碼獲取新浪微博應(yīng)用的access token代碼實(shí)例
這篇文章主要介紹了使用java代碼獲取新浪微博應(yīng)用的access token實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn),TTL就是消息或者隊(duì)列的過(guò)期功能,當(dāng)消息過(guò)期就會(huì)進(jìn)到死信隊(duì)列,死信隊(duì)列和普通隊(duì)列沒(méi)啥區(qū)別,然后我們只需要配置一個(gè)消費(fèi)者來(lái)消費(fèi)死信隊(duì)列里面的消息就可以了,需要的朋友可以參考下2023-08-08
java Volatile與Synchronized的區(qū)別
這篇文章主要介紹了java Volatile與Synchronized的區(qū)別,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
通過(guò)一個(gè)map替換字符串中指定的字符變量方法
下面小編就為大家?guī)?lái)一篇通過(guò)一個(gè)map替換字符串中指定的字符變量方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03

