spring boot的yaml配置檔案用法

2021-08-20 19:08:52 字數 1222 閱讀 1707

1、spring boot的配置檔案一般使用的有兩種格式:

spring boot會自動載入配置檔案。

2、ymal(yaml ain't a markup language),以前大多使用的***.xml檔案,yaml是以資料為中心,相對於json、xml更適合作為配置檔案。

server:port:8081
3、ymal基本語法規則:

一般map、物件、鍵值對的寫法:      

#一般使用這種寫法

friends:name:zhangsan

age:20

#這種事行內寫法

friends:

陣列(list、set)用法:

用- 值表示陣列中的乙個元素:

#一般寫法

pets:- dog

- cat

- pig

#行內寫法

pets:[dag,cat,pig]

4、將yml檔案中配置的資料對映繫結到類上:需要加上註解

@configurationproperties(prefix = "friends")

public class friends

public string getname()

public void

setage(integer age)

@override

public string tostring() ';}

}

同時還要在pom.xml檔案中匯入依賴:

org.springframework.boot

spring-boot-configuration-processor

true

5、還可以使用@value註解來進行繫結對映:

@value("$")

private string name

;@value("$")

private integer age

;

Spring Boot 使用YAML配置

server port 8081 db configuration spring datasource driverclassname com.mysql.jdbc.driver url jdbc mysql localhost 3307 microservice username root pas...

Spring Boot中的yaml的基本語法與配置

spring boot中的yaml的基本語法與配置 1.yaml概念 可讀性高,用來表達資料序列化的格式,字尾 yml 或者yaml 如果 並行存在,優先順序是後載入的,替換先載入的。一般配置檔案都是,有時有多個環境,比如有開發環境 測試環境 生產環境 1 自定義banner 2.基本語法 大小寫敏...

Spring boot原始碼分析 yaml語言(9)

yaml j m l 和camel同發音 是人類可讀的 資料序列化語言。它通常用於配置檔案js yaml yaml表示 json表示 備註資訊 使用內嵌塊的表示方式 物件 name petspeople 水果 蘋果 西瓜 桃子水果 蘋果,西瓜,桃子,果汁 資料 曾經有乙份真摯的愛情 擺在我的面前 門...