SpringBoot yml配置文件讀取方法詳解
yaml介紹
YAML(YAML Ain't Markup Language),一種數(shù)據(jù)序列化格式
優(yōu)點(diǎn):
- 容易閱讀
- 容易與腳本語言交互
- 以數(shù)據(jù)為核心,重?cái)?shù)據(jù)輕格式
YANL文件擴(kuò)展名
- .yml(主流)
- .yaml
幾種數(shù)據(jù)格式比較

yaml語法規(guī)則
- 大小寫敏感
- 屬性層級(jí)關(guān)系使用多行描述,每行結(jié)尾使用冒號(hào)結(jié)束
- 使用縮進(jìn)表示層級(jí)關(guān)系,同層級(jí)左側(cè)對(duì)齊,只允許使用空格(不允許使用Tab鍵)
- 屬性值前面添加空格(屬性名與屬性值之間使用冒號(hào)+空格作為分隔)
- #表示注釋
示例:
user:
name: zhangsan
age: 12
users:
-
name: lisi
age: 13
-
name: wangwu
age: 18
likes: [game,play,having]
users1: [{name:zhangsan,age:12},{name:lisi,age:12}]
字面值表示方式

數(shù)組表示方式:在屬性名書寫位置的下方使用減號(hào)作為數(shù)據(jù)開始符號(hào),每行書寫一個(gè)數(shù)據(jù),減號(hào)與數(shù)據(jù)鍵空格分隔

yaml數(shù)據(jù)讀取
使用@Value讀取單個(gè)數(shù)據(jù),屬性名引用方式:${一級(jí)屬性名.二級(jí)屬性名}

controller下
package com.springboot01_02.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/SpringBoot")
public class TestController {
@Value("${user.name1}")
private String username1;
@Value("${users[0].age}")
private String userage2;
@Value("${person.hobby[0]}")
private String personHobbyEat;
@Value("${person.hobby[1]}")
private String personHobbyPlay;
@Value("${users1[0].age}")
private String usersAge;
@RequestMapping("/test")
public String Test(){
System.out.println("username->"+username1);
System.out.println("userage->"+userage2);
System.out.println("personHobbyEat->"+personHobbyEat);
System.out.println("personHobbyPlay->"+personHobbyPlay);
System.out.println("usersAge->"+usersAge);
return "springboot is good";
}
}yml配置文件
user:
name1: KC
age: 12
users:
-
name: lisi
age: 13
-
name: wangwu
age: 18
person:
name: ZH
age: 19
tel: 152161
hobby:
- eat
- play
- run
users1: [{name: zhangsan,age: 12},{name: lisi,age: 12}]
likes: [game,play,having]
運(yùn)行結(jié)果:

yaml數(shù)據(jù)讀取
在配置文件中可以使用屬性名引用方式引用屬性
在配置文件中

package com.springboot01_02.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/SpringBoot")
public class TestController {
@Value("${nowDir}")
private String nowDir1;
@Value("${tewDir}")
private String tewDir1;
@RequestMapping("/test")
public String Test(){
System.out.println("nowDir->"+nowDir1);
System.out.println("towDir->"+tewDir1);
return "springboot is good";
}
}運(yùn)行結(jié)果:

可以發(fā)現(xiàn),要想讓轉(zhuǎn)義字符生效,就得加上雙引號(hào)不然還是以字符串的形式打印出
Environment讀取yaml全部屬性數(shù)據(jù)
package com.springboot01_02.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/SpringBoot")public class TestController {//使用自動(dòng)裝配將所有數(shù)據(jù)封裝到一個(gè)Environment中 @Autowired private Environment evn; @RequestMapping("/test") public String Test(){ System.out.println("----------------"); System.out.println(evn.getProperty("nowDir")); System.out.println(evn.getProperty("users1[0].age")); return "springboot is good"; }}
運(yùn)行結(jié)果:

小結(jié):
使用Environment對(duì)象封裝全部配置信息
使用@Autowired自動(dòng)裝配數(shù)據(jù)到Environment對(duì)象中
自定義對(duì)象封裝指定數(shù)據(jù)
application.yaml配置文件中的信息
#創(chuàng)建類,用于封裝下面的數(shù)據(jù)#有spring帶我們?nèi)ゼ虞d數(shù)據(jù)到對(duì)象中,且告訴spring加載這組信息#使用時(shí)從spring中直接獲取信息使用datasource: driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/love username: kongchao password: zenghui
自定義一個(gè)類
package com.springboot01_02.datesource;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;//1、定義數(shù)據(jù)類型模型封裝yaml文件中對(duì)應(yīng)的數(shù)據(jù)//2、定義為spring管控的bean@Component//3、指定加載的數(shù)據(jù)@ConfigurationProperties("datasource")//4、設(shè)置getSet方法等@Datapublic class MyDateSource { private String driver; private String url; private String username; private String password;}
使用了@Date,在pom.xml中導(dǎo)入lombok
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
測(cè)試類下
package com.springboot01_02.controller;import com.springboot01_02.datesource.MyDateSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/SpringBoot")public class TestController {@Autowired private MyDateSource dateSource; @RequestMapping("/test") public String Test(){ System.out.println(dateSource); return "springboot is good"; }}
運(yùn)行訪問localhost/SpringBoot/test即可得到:

小結(jié):
使用@ConfigurationProperties注解綁定配置信息到封裝類中
封裝類需要定義為Spring管理的bean(使用注解@Component),否則無法進(jìn)行屬性注入
到此這篇關(guān)于SpringBoot yml配置文件讀取方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot讀取yml配置 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot項(xiàng)目集成swagger-bootstrap-ui全過程
這篇文章主要介紹了springboot項(xiàng)目集成swagger-bootstrap-ui全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
MybatisPlus特殊查詢的實(shí)現(xiàn)介紹
這篇文章主要介紹了MybatisPlus查詢投影、聚合查詢、分組查詢、等值查詢、范圍查詢、模糊查詢、排序查詢,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
Java Vector和ArrayList的異同分析及實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Java Vector和ArrayList的異同分析及實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01

