SpringBoot yaml语法与数据读取操作详解
作者:不会压弯的小飞侠
yaml
YAML是一种数据序列化格式。
yaml扩展名
- .yaml
- .yml(主流)
yaml语法规则
- 大小写敏感
- 属性层次关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- #表示注释
字面值表示方式:
boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期与时间用T连接
datetime: 2022-7-9T12:00:30+02:00
数组表示方式:
likes:
- music
- draw
- gamelikes1: [music,draw,game]
对象数组格式:
user2:
- name: xiaofeixia
age: 22
- name: xiaomage
age: 26user3:
-
name: xiaofeixia
age: 22
-
name: xiaomage
age: 27
对象数组缩略格式:
user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]
读取yaml数据
使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}
编写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}]
读取单一数据
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数据中的单一数据 @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..."; } }
读取二级数据
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..."; } }
读取数组数据
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..."; } }
读取服务器端口号
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..."; } }
读取对象属性
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..."; } }
封装全部数据到Environment对象
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引用类型属性数据
application.yml
server:
port: 81
#创建类用于封装下面的数据
#由spring去加载数据到对象中,一定要告诉spring加载这组信息
#使用的时候直接从spring中获取信息
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springboot
username: root
password: root
MyDataSource
自定义对象封装指定数据
1.定义数据模型封装yaml文件中对应的数据
package com.jkj; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; //2.定义spring的管控Bean @Component //3.指定加载数据 @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 + '\'' + '}'; } }
读取数据
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
读取数据
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
只写:
server:
port: 81
控制台输出:path为空
加上context-path后:
server:
port: 81
servlet:
context-path: /test
控制台输出页面:
注意:在浏览器输入:http://localhost:81/test/yamlBooks 进行测试。
@Autowired报错解决方案
- file
- setting
- 搜索Spring Core
- 如下图所示将Severity修改为Warning
到此这篇关于SpringBoot yaml语法与数据读取操作详解的文章就介绍到这了,更多相关SpringBoot yaml语法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!