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>
測試類下
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)文章
java操作gaussDB數(shù)據(jù)庫的實(shí)現(xiàn)示例
本文主要介紹了java操作gaussDB數(shù)據(jù)庫的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot?ScheduledTaskRegistrar解決動(dòng)態(tài)定時(shí)任務(wù)思路詳解
本文將從問題出發(fā),詳細(xì)介紹ScheduledTaskRegistrar類是如何解決動(dòng)態(tài)調(diào)整定時(shí)任務(wù)的思路,并給出關(guān)鍵的代碼示例,幫助大家快速地上手學(xué)習(xí)2023-02-02關(guān)于spring中aop的注解實(shí)現(xiàn)方法實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于spring中aop的注解實(shí)現(xiàn)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08MyBatis測試報(bào)錯(cuò):Cannot?determine?value?type?from?string?&a
這篇文章主要給大家介紹了關(guān)于MyBatis測試報(bào)錯(cuò):Cannot?determine?value?type?from?string?'xxx'的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Java中遍歷Map的多種方法示例及優(yōu)缺點(diǎn)總結(jié)
在java中遍歷Map有不少的方法,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷Map的多種方法,以及各種方法的優(yōu)缺點(diǎn)總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07Java將對(duì)象寫入文件讀出_序列化與反序列化的實(shí)例
下面小編就為大家?guī)硪黄狫ava將對(duì)象寫入文件讀出_序列化與反序列化的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Java應(yīng)用程序的CPU使用率飆升原因詳細(xì)分析
這篇文章主要介紹了Java應(yīng)用程序的CPU使用率飆升原因詳細(xì)分析,在 Java 中,我們使用 JVM 進(jìn)行線程調(diào)度,所以一般來說,線程的調(diào)度有兩種模式:分時(shí)調(diào)度和搶占式調(diào)度,線程和進(jìn)程在阻塞或者等待時(shí),都不會(huì)使用 CPU 資源,需要的朋友可以參考下2024-01-01