yml配置文件的读取
YAML(/ˈjæməl/,尾音类似camel骆驼)是 “YAML Ain’t a Markup Language”(YAML不是一种标记语言)的递归缩写,是一个可读性高,用来表达数据序列化的格式。
一、yml基本语法
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格
- 不同级之间缩进2格空格
- 冒号后面有一个空格,前面没有
- 注释以#开头
二、yml数据类型
1、基本数据类型:
-
String:字符串,格式:
ymlstring: hello ymlString
-
number:整数和浮点型,直接以字面量的形式表示:
ymlnumber: 12.30
-
boolean:布尔值用true和false表示:
isYml: true
-
null:null用~表示:
yml: ~
2、其他数据类型:
1、对象: Java用一个对象来接收
- 格式:ymlobject是前缀,code、msg、errormsg对应对象的属性字段,必须一致,后面是属性读取的值。
ymlobject: code: 2000 msg: 成功返回 errormsg: 错误信息
2、List: Java用List集合接收
- 格式:此集合中包含2个元素,3000和string
ymlList: - 3000 - string
3、Map: Java用Map集合接收
-
格式:此Map中包含两对键值对,key1: value1和key2: value2
ymlmap: map: {key1: value1,key2: value2}
三、Spring读取yml配置信息
我使用的是springboot来做的测试,全部注解的方式,如有不同请自行网上查询其他资料。
1、基本数据类型的接收:
-
yml配置文件:
ymlstring: hello ymlString
-
配置类:
/** * 配置类读取yml文件中的string * @Author: smile * @Date: 2020/1/17 */ @Component @ConfigurationProperties public class YmlStringTest { private String ymlstring; public String getYmlstring() { return ymlstring; } public void setYmlstring(String ymlstring) { this.ymlstring = ymlstring; } }
-
注意yml文件的前缀要跟配置类中的string属性名相同,其他基本类型同string,不演示了。
2、对象的接收:
-
yml配置文件:
ymlobject: code: 2000 msg: 成功返回 errormsg: 错误信息
-
配置类:
/** * 配置类读取yml文件中的对象 * @Author: smile * @Date: 2020/1/17 */ @Component @ConfigurationProperties(prefix = "ymlobject") public class YmlObjectTest { private int code; private String msg; private String errormsg; //省略getter和setter方法 }
-
接收对象需要在@ConfigurationProperties注解上加前缀名prefix = “ymlobject”,不识别大写,必须全部用小写,且与yml中的一致。
3、List集合的接收:
-
yml配置文件:
ymlList: - 3000 - string
-
配置类:
/** * 配置类读取yml文件中的List集合 * @Author: smile * @Date: 2020/1/17 */ @Component @ConfigurationProperties public class YmlListTest { private List ymlList; public List getYmlList() { return ymlList; } public void setYmlList(List ymlList) { this.ymlList = ymlList; } }
-
yml文件中的前缀要跟集合的名称相同,即ymlList
4、Map集合的接收:
-
yml配置文件:
ymlmap: map: {key1: value1,key2: value2}
-
配置类:
/** * 配置类读取yml文件中的Map集合 * @Author: smile * @Date: 2020/1/17 */ @Component @ConfigurationProperties(prefix = "ymlmap") public class YmlMapTest { private Map<String,String> map; public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
-
接收Map集合需要在@ConfigurationProperties注解上加前缀名prefix = “ymlmap”,不识别大写,必须全部用小写,且与yml中的一致。
5、组合嵌套接收:
-
yml配置文件:
ymltogether: code: 2000 msg: 获取配置信息ok ymlObjectTest: code: 2000 msg: 成功返回 errormsg: 错误信息 ymlListTest: - 3000 - string
-
配置类:
/**
* 配置类读取yml文件中的组合对象
* @Author: smile
* @Date: 2020/1/17
*/
@Component
@ConfigurationProperties(prefix = "ymltogether")
public class YmlTogetherTest {
private int code;
private String msg;
private YmlObjectTest ymlObjectTest;
private YmlListTest ymlListTest;
//省略getter和setter方法
}
- 接收组合对象需要在@ConfigurationProperties注解上加前缀名prefix = “ymltogether”,不识别大写,必须全部用小写,且与yml中的一致。
6、在controller层接收:
在controller层直接使用注解@Value即可获取到yml文件的信息
-
yml配置文件:
comment: avatar: /images/avatar.jpg #自定义图片地址,可以通过自动注入取值
-
使用方法:
@Value("${comment.avatar}") private String avatar;
以上读取的都是springboot默认配置文件appication.yml中的信息,如何读取自定义配置文件 还未做测试。