1. yml简介
# YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。
## 在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),
## 但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
# yml语法
# 0. YAML 使用可打印的Unicode字符,可以使用utf-8 utf-16
# 1. 使用 “k: v” 格式表示键值对关系,冒号后面必须有一个空格
# 2. 使用空格(“ ”)缩进表示层级关系,不能使用跳格字符(TAB); 空格数目不重要,只要左对齐的数据,表示都在同一层
# 3. 使用单引号(‘’)双引号(“”) 字符串类型的值 平常不需要使用引号, 有特殊字符需要处理的时候需要加双引号或者单引号
# 4. 使用井号 (#) 表示单行注释
# 5. 使用双感叹号 (!!) 强制指定格式(数据类型)
# 6. 使用三横杆 (---) 分割文档块,表示多个文件
# 7. 使用三点号 (...) 表示文档结尾
# 8. 使用一组横杠(-)或者中括号[]加逗号分割的元素 表示清单列表(数组,list,set)
# 9. 使用大括号({})表示散列表(map 子对象),散列表的键值可以使用问号(?)开头,表示多个词汇组成的键值
# 10. 文档中重复的部分使用锚点符号(&)和引用符号(*),表示将&标注的内容复制引用到*标注的地方
# 11. 区块字符串用缩进和修饰符(非必要)来和其他数据分隔。有新行保留(preserve)使用符号(|);新行折叠(flod)使用符号(>)
2. 使用示例
2.1 application.yml
# 示例如下:
system:
# 对应数据类型 String(字符串的值直接书写)
name: \tPC_123345
# 对应数据类型 String(字符串的值加 单引号)
name1: '\tPC_123345'
# 对应数据类型 String(字符串的值 加双引号 可以使用(\)开始的转义字符)
name2: "\tPC_123345"
# 区块字符串 不加修饰符
name3:
11
22
33
# 区块字符串 加|
name4: |
one
two
three
# 区块字符串 加 >
name5: >
一
二
三
# !!str 表示严格的字符串
address: !!str 127.0.0.1
# 对应数据类型 Integer
cpu: 16
# 对应数据类型 Boolean
enabled: true
# 对应数据类型 Date
createTime: 2020/11/21
# 宽松绑定原则(root-path与 rootPath都可以绑定到 rootPath属性上)
root-path: C:\Program Files
# 对应数据类型 Map 子对象(行内写法)
display: {
name: 显示屏幕一, scalingLayout: 100%, resolution: 1920*1080 }
# 对应数据类型 数组(行内写法)
devices: [ 键盘, 鼠标, 显示器 ]
# 对应数据类型 list set 数组
users:
- username: user1
password: 111111
- username: user2
password: 222222
- {
username: user3, password: 333333 } #(行内写法)
permissions:
- select
- delete
- insert
userGroups:
- group1
- group2
# 系统环境的值
spring:
profiles:
active: ${
spring.profiles.active}
# 文档块
---
spring:
config:
activate:
on-profile: dev
system:
desc: 这是开发环境
---
spring:
config:
activate:
on-profile: fat
system:
desc: 这是测试环境
---
spring:
config:
activate:
on-profile: prod
system:
desc: 这是生产环境
# ... 表示文档结尾
...
2.2 Java对象
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Set;
@Data
@Component
@ConfigurationProperties(prefix = "system")
public class SystemConfig {
private String name;
private String name1;
private String name2;
private String name3;
private String name4;
private String name5;
private String address;
private Integer cpu;
private Boolean enabled;
private Date createTime;
private String rootPath;
private Display display;
private String[] devices;
private List<User> users;
private Set<String> permissions;
private String[] userGroups;
private String desc;
@Value("${spring.profiles.active}")
private String active;
@Data
static class User {
private String username;
private String password;
}
@Data
static class Display {
private String name;
private String scalingLayout;
private String resolution;
}
}
2.3 测试接口
/**
* 测试 yml配置
*/
@RestController
public class TestConfigController {
@Autowired
private SystemConfig systemConfig;
/**
* 测试 @ConfigurationProperties
*
* @return 配置文件的json格式
*/
@RequestMapping("/testConfig")
public Object testConfig() {
System.out.println(systemConfig.toString());
return systemConfig;
}
}
2.4 配置启动变量

2.5测试结果
{
"name": "\\tPC_123345",
"name1": "\\tPC_123345",
"name2": "\tPC_123345",
"name3": "11 22 33",
"name4": "one\ntwo\nthree\n",
"name5": "一 二 三\n",
"address": "127.0.0.1",
"cpu": 16,
"enabled": true,
"createTime": "2020-11-20T16:00:00.000+00:00",
"rootPath": "C:\\Program Files",
"display": {
"name": "显示屏幕一",
"scalingLayout": "100%",
"resolution": "1920*1080"
},
"devices": [
"键盘",
"鼠标",
"显示器"
],
"users": [
{
"username": "user1",
"password": "111111"
},
{
"username": "user2",
"password": "222222"
},
{
"username": "user3",
"password": "333333"
}
],
"permissions": [
"select",
"delete",
"insert"
],
"userGroups": [
"group1",
"group2"
],
"desc": "这是开发环境",
"active": "dev"
}
还没有评论,来说两句吧...