SpringBootyml配置文件讀取方法詳解_第1頁(yè)
SpringBootyml配置文件讀取方法詳解_第2頁(yè)
SpringBootyml配置文件讀取方法詳解_第3頁(yè)
SpringBootyml配置文件讀取方法詳解_第4頁(yè)
SpringBootyml配置文件讀取方法詳解_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第SpringBootyml配置文件讀取方法詳解目錄yaml介紹yaml語(yǔ)法規(guī)則yaml數(shù)據(jù)讀取Environment讀取yaml全部屬性數(shù)據(jù)自定義對(duì)象封裝指定數(shù)據(jù)

yaml介紹

YAML(YAMLAintMarkupLanguage),一種數(shù)據(jù)序列化格式

優(yōu)點(diǎn):

容易閱讀容易與腳本語(yǔ)言交互以數(shù)據(jù)為核心,重?cái)?shù)據(jù)輕格式

YANL文件擴(kuò)展名

.yml(主流).yaml

幾種數(shù)據(jù)格式比較

yaml語(yǔ)法規(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下

packagecom.springboot01_02.controller;

importorg.springframework.beans.factory.annotation.Value;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/SpringBoot")

publicclassTestController{

@Value("${1}")

privateStringusername1;

@Value("${users[0].age}")

privateStringuserage2;

@Value("${person.hobby[0]}")

privateStringpersonHobbyEat;

@Value("${person.hobby[1]}")

privateStringpersonHobbyPlay;

@Value("${users1[0].age}")

privateStringusersAge;

@RequestMapping("/test")

publicStringTest(){

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"springbootisgood";

}

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ù)讀取

在配置文件中可以使用屬性名引用方式引用屬性

在配置文件中

packagecom.springboot01_02.controller;

importorg.springframework.beans.factory.annotation.Value;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/SpringBoot")

publicclassTestController{

@Value("${nowDir}")

privateStringnowDir1;

@Value("${tewDir}")

privateStringtewDir1;

@RequestMapping("/test")

publicStringTest(){

System.out.println("nowDir-"+nowDir1);

System.out.println("towDir-"+tewDir1);

return"springbootisgood";

}

運(yùn)行結(jié)果:

可以發(fā)現(xiàn),要想讓轉(zhuǎn)義字符生效,就得加上雙引號(hào)不然還是以字符串的形式打印出

Environment讀取yaml全部屬性數(shù)據(jù)

packagecom.springboot01_02.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.core.env.Environment;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping(/SpringBoot)publicclassTestController{//使用自動(dòng)裝配將所有數(shù)據(jù)封裝到一個(gè)Environment中@AutowiredprivateEnvironmentevn;@RequestMapping(/test)publicStringTest(){System.out.println(----------------System.out.println(evn.getProperty(nowDir));System.out.println(evn.getProperty(users1[0].age));returnspringbootisgood}}

運(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.Driverurl:jdbc:mysql://localhost/loveusername:kongchaopassword:zenghui

自定義一個(gè)類

packagecom.springboot01_02.datesource;importlombok.Data;importperties.ConfigurationProperties;importorg.springframework.stereotype.Component;//1、定義數(shù)據(jù)類型模型封裝yaml文件中對(duì)應(yīng)的數(shù)據(jù)//2、定義為spring管控的bean@Component//3、指定加載的數(shù)據(jù)@ConfigurationProperties(datasource)//4、設(shè)置getSet方法等@DatapublicclassMyDateSource{privateStringdriver;privateStringurl;privateStringusername;privateStringpassword;}

使用了@Date,在pom.xml中導(dǎo)入lombok

dependencygroupIdjectlombok/groupIdartifactIdlombok/artifactIdversion1.16.10/version/dependency

測(cè)試類下

packagecom.springboot01_02.controller;importcom.springboot01_02.datesource.MyDateSource;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping(/SpringBoot)publicclassTestController{@AutowiredprivateMyDateSourcedateSource;@RequestMap

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論