@PropertySource
注解加载yml
文件背景: 业务须要建立多个配置类,基于 yml
文件拥有简洁的层次结构,遂配置文件选择 yml
类型。 但在实际的开发中遇到使用 @PropertySource
注解没法加载 yml
配置文件问题。spring
首先咱们先来分析一下 @PropertySource
注解的源码:bash
public @interface PropertySource {
/** 加载资源的名称 */
String name() default "";
/**
* 加载资源的路径,可以使用classpath,如:
* "classpath:/config/test.yml"
* 若有多个文件路径放在{}中,使用','号隔开,如:
* {"classpath:/config/test1.yml","classpath:/config/test2.yml"}
* 除使用classpath外,还可以使用文件的地址,如:
* "file:/rest/application.properties"
*/
String[] value();
/** 此属性为根据资源路径找不到文件后是否报错, 默认为是 false */
boolean ignoreResourceNotFound() default false;
/** 此为读取文件的编码, 若配置中有中文建议使用 'utf-8' */
String encoding() default "";
/**
* 关键:此为读取资源文件的工程类, 默认为:
* 'PropertySourceFactory.class'
*/
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
复制代码
从源码能够看出,读取资源文件 PropertySourceFactory
接口是关键,加下来打开 PropertySourceFactory
接口的源码:app
public interface PropertySourceFactory {
PropertySource<?> createPropertySource(@Nullable String var1, EncodedResource var2) throws IOException;
}
复制代码
发现其中只有一个建立属性资源接口的方法,接下来咱们找到实现这个方法的类:ide
public class DefaultPropertySourceFactory implements PropertySourceFactory {
public DefaultPropertySourceFactory() {
}
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
}
}
复制代码
在这个类中咱们发现其返回了一个对象 ResourcePropertySource
,找到 DefaultPropertySourceFactory
类使用的两个 ResourcePropertySource
类的构造方法:测试
public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
super(name, PropertiesLoaderUtils.loadProperties(resource));
this.resourceName = getNameForResource(resource.getResource());
}
public ResourcePropertySource(EncodedResource resource) throws IOException {
super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
this.resourceName = null;
}
复制代码
在上面代码中,两个构造方法都使用了 PropertiesLoaderUtils.loadProperties()
这个属性的方法, 一直点下去, 会发现这么一段代码:ui
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
throws IOException {
InputStream stream = null;
Reader reader = null;
try {
String filename = resource.getResource().getFilename();
// private static final String XML_FILE_EXTENSION = ".xml";
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
stream = resource.getInputStream();
persister.loadFromXml(props, stream);
}
else if (resource.requiresReader()) {
reader = resource.getReader();
persister.load(props, reader);
}
else {
stream = resource.getInputStream();
persister.load(props, stream);
}
}
finally {
if (stream != null) {
stream.close();
}
if (reader != null) {
reader.close();
}
}
}
复制代码
由上可知,@PropertySource
注解也能够用来加载 xml
文件,接下来根据 persister.load(props, stream)
方法一直点下去会找到下面一段代码:this
private void load0 (LineReader lr) throws IOException {
char[] convtBuf = new char[1024];
int limit;
int keyLen;
int valueStart;
char c;
boolean hasSep;
boolean precedingBackslash;
/**
* 每次读取一行
*/
while ((limit = lr.readLine()) >= 0) {
c = 0;
keyLen = 0;
valueStart = limit;
hasSep = false;
//System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
precedingBackslash = false;
/**
* 遍历一行字的每个字符
* 若字符中出现 '='、':'、' '、'\t'、'\f' 则跳出循环
*/
while (keyLen < limit) {
c = lr.lineBuf[keyLen];
//need check if escaped.
// 若是当前遍历字符为 '=' 或 ':' 则跳出循环
if ((c == '=' || c == ':') && !precedingBackslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
}
// 若是当前遍历字符为 ' ' 或 '\t' 或 '\f' 跳出循环,
// 但在接下来的循环中还须要继续遍历知道找到 '=' 或 ':'
else if ((c == ' ' || c == '\t' || c == '\f') && !precedingBackslash) {
valueStart = keyLen + 1;
break;
}
// 检查是否转义
if (c == '\\') {
precedingBackslash = !precedingBackslash;
} else {
precedingBackslash = false;
}
// 每次循环,keyLen + 1
keyLen++;
}
/**
* 判断valueStart(值的开始下标)是否小于读取行的长度,若小于,则进入循环
*/
while (valueStart < limit) {
c = lr.lineBuf[valueStart];
// 判断当前字符是否等于空格、制表符、换页符。都不等于则进入循环
if (c != ' ' && c != '\t' && c != '\f') {
// 当 hasSep 为false时表明上个 while (keyLen < limit) 循环跳出时c为 空格或制表符或换页符
// 这里继续循环直到找到'='或':'号为止
// 因而可知 在配置文件中'=' 或 ':' 号前可有空格、制表符、换页符
if (!hasSep && (c == '=' || c == ':')) {
hasSep = true;
} else {
break;
}
}
// 每次循环,valueStart + 1
valueStart++;
}
// 获取配置文件中的key,value并保存
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, value);
}
}
复制代码
上面 load0
方法每次读取一行,而后根据 '='
或 ':'
来获取 key
和 value
,而 yml
具备鲜明层次结构的特色则不能由此方法读取。编码
@PropertySource
注解读取属性文件的关键在于 PropertySourceFactory
接口中的 createPropertySource
方法,因此咱们想要实现 @PropertySource
注解读取 yml
文件就须要实现 createPropertySource
方法,在 @PropertySource
注解其是经过 DefaultPropertySourceFactory
类来实现这个方法,咱们只须要继承此类,并重写其 createPropertySource
方法便可,实现代码以下:@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null){
return super.createPropertySource(name, resource);
}
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
复制代码
注: spring boot
中 yml
、yaml
对应的加载类为 YamlPropertySourceLoader
。spa
@Component
@PropertySource(value = "test.yml", encoding = "utf-8", factory = TestFactory.class)
@ConfigurationProperties(prefix = "com.test")
public class IdCardServerConfig {
private String serverCode;
...
}
复制代码