SpringBoot yaml語(yǔ)法與數(shù)據(jù)讀取操作詳解
yaml
YAML是一種數(shù)據(jù)序列化格式。
yaml擴(kuò)展名
- .yaml
- .yml(主流)
yaml語(yǔ)法規(guī)則
- 大小寫(xiě)敏感
- 屬性層次關(guān)系使用多行描述,每行結(jié)尾使用冒號(hào)結(jié)束
- 使用縮進(jìn)表示層級(jí)關(guān)系,同層左側(cè)對(duì)齊,只允許使用空格(不允許使用Tab鍵)
- 屬性值前面添加空格(屬性名與屬性值之間使用冒號(hào)+空格作為分隔)
- #表示注釋
字面值表示方式:
boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期與時(shí)間用T連接
datetime: 2022-7-9T12:00:30+02:00
數(shù)組表示方式:
likes:
- music
- draw
- gamelikes1: [music,draw,game]
對(duì)象數(shù)組格式:
user2:
- name: xiaofeixia
age: 22
- name: xiaomage
age: 26user3:
-
name: xiaofeixia
age: 22
-
name: xiaomage
age: 27
對(duì)象數(shù)組縮略格式:
user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]
讀取yaml數(shù)據(jù)
使用@Value讀取單個(gè)數(shù)據(jù),屬性名引用方式:${一級(jí)屬性名.二級(jí)屬性名}
編寫(xiě)yaml文件
server:
port: 81
country: china
province: henan
city: zhengzhou
area: shangqiuparty: true
birthday: 2022-11-11user8:
name: xiaofeixia
age: 22
user1:
name: xiaofeixia
age: 22a:
B:
C:
d:
e: abclikes:
- music
- draw
- gamelikes1: [music,draw,game]
user2:
- name: xiaofeixia
age: 22
- name: xiaomage
age: 26user3:
-
name: xiaofeixia
age: 22
-
name: xiaomage
age: 27user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]
讀取單一數(shù)據(jù)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
//讀取yaml數(shù)據(jù)中的單一數(shù)據(jù)
@Value("${country}")
public String country1;
@GetMapping
public String ById(){
System.out.println("springboot is running...");
System.out.println("country=="+country1); //country==china
return "springboot is running...";
}
}讀取二級(jí)數(shù)據(jù)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Value("${user8.name}")
public String username;
@GetMapping
public String ById(){
System.out.println("springboot is running...");
System.out.println("username=="+username); //username==xiaofeixia
return "springboot is running...";
}
}讀取數(shù)組數(shù)據(jù)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Value("${likes[0]}")
public String likes1;
@GetMapping
public String ById(){
System.out.println("springboot is running...");
System.out.println("likes1=="+likes1); //likes1==music
return "springboot is running...";
}
}讀取服務(wù)器端口號(hào)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Value("${server.port}")
public String port;
@GetMapping
public String ById(){
System.out.println("springboot is running...");
System.out.println("port=="+port); //port==81
return "springboot is running...";
}
}讀取對(duì)象屬性
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Value("${user2[0].age}")
public String age2;
@GetMapping
public String ById(){
System.out.println("springboot is running...");
System.out.println("age2=="+age2); //age2==22
return "springboot is running...";
}
}封裝全部數(shù)據(jù)到Environment對(duì)象
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Autowired
private Environment env;
@GetMapping
public String ById(){
System.out.println(env.getProperty("server.port"));
System.out.println(env.getProperty("user8.name"));
return "springboot is running...";
}
}讀取yaml引用類型屬性數(shù)據(jù)
application.yml
server:
port: 81
#創(chuàng)建類用于封裝下面的數(shù)據(jù)
#由spring去加載數(shù)據(jù)到對(duì)象中,一定要告訴spring加載這組信息
#使用的時(shí)候直接從spring中獲取信息
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springboot
username: root
password: root
MyDataSource
自定義對(duì)象封裝指定數(shù)據(jù)
1.定義數(shù)據(jù)模型封裝yaml文件中對(duì)應(yīng)的數(shù)據(jù)
package com.jkj;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//2.定義spring的管控Bean
@Component
//3.指定加載數(shù)據(jù)
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}讀取數(shù)據(jù)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Autowired
private MyDataSource myDataSource;
@GetMapping
public String ById(){
System.out.println(myDataSource);
//MyDataSource{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://localhost/springboot', username='root', password='root'}
return "springboot is running...";
}
}變量的引用
application.yml
server:
port: 81
baseDir: E:\window
tempDir: ${baseDir}\temp
讀取數(shù)據(jù)
package com.jkj.controller;
import com.jkj.MyDataSource;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
@Value("${tempDir}")
public String temp;
@GetMapping
public String ById(){
System.out.println("temp=="+temp); //temp==E:\window\temp
return "springboot is running...";
}
}context-path
只寫(xiě):
server:
port: 81
控制臺(tái)輸出:path為空

加上context-path后:
server:
port: 81
servlet:
context-path: /test
控制臺(tái)輸出頁(yè)面:

注意:在瀏覽器輸入:http://localhost:81/test/yamlBooks 進(jìn)行測(cè)試。
@Autowired報(bào)錯(cuò)解決方案
- file
- setting
- 搜索Spring Core
- 如下圖所示將Severity修改為Warning

到此這篇關(guān)于SpringBoot yaml語(yǔ)法與數(shù)據(jù)讀取操作詳解的文章就介紹到這了,更多相關(guān)SpringBoot yaml語(yǔ)法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot YAML語(yǔ)法基礎(chǔ)詳細(xì)整理
- SpringBoot yaml語(yǔ)法與JRS303校驗(yàn)超詳細(xì)講解
- SpringBoot中YAML語(yǔ)法及幾個(gè)注意點(diǎn)說(shuō)明
- Springboot整合freemarker和相應(yīng)的語(yǔ)法詳解
- Java springboot yaml語(yǔ)法注解
- SpringBoot中的yaml語(yǔ)法及靜態(tài)資源訪問(wèn)問(wèn)題
- SpringBoot SpEL語(yǔ)法掃盲與查詢手冊(cè)的實(shí)現(xiàn)
- springboot常用語(yǔ)法庫(kù)的基本語(yǔ)法
相關(guān)文章
java基于JDBC連接Oracle 11g Release2實(shí)例分析
這篇文章主要介紹了java基于JDBC連接Oracle 11g Release2的方法,實(shí)例分析了JDBC連接Oracle 11g Release2容易出現(xiàn)的異常與解決方法,需要的朋友可以參考下2015-06-06
淺談Android開(kāi)發(fā)中項(xiàng)目的文件結(jié)構(gòu)及規(guī)范化部署建議
這篇文章主要介紹了Android開(kāi)發(fā)中項(xiàng)目的文件結(jié)構(gòu)及規(guī)范化部署建議,組織好代碼文件的結(jié)構(gòu)有利于維護(hù)團(tuán)隊(duì)合作的效率,需要的朋友可以參考下2016-03-03
java WebSocket的實(shí)現(xiàn)以及Spring WebSocket示例代碼
本篇文章主要介紹了java WebSocket的實(shí)現(xiàn)以及Spring WebSocket,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Spring?Cloud實(shí)現(xiàn)灰度發(fā)布的示例代碼
這篇文章主要為大家詳細(xì)介紹了Spring?Cloud實(shí)現(xiàn)灰度發(fā)布的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-09-09
Java警告:原發(fā)性版11需要目標(biāo)發(fā)行版11的解決方法和步驟
這篇文章主要介紹了Java警告:原發(fā)性版11需要目標(biāo)發(fā)行版11的解決方法和步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-04-04
Java Spring中Quartz調(diào)度器詳解及實(shí)例
這篇文章主要介紹了Java Spring中Quartz調(diào)度器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02

