0
点赞
收藏
分享

微信扫一扫

SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码

@PropertySource源码:


SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_资源文件

在这里插入图片描述


根据注释,默认使用DefaultPropertySourceFactory类作为资源文件加载类


SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_spring框架_02

在这里插入图片描述


里面还是调用Spring框架底层的PropertiesLoaderUtils工具类进行读取的

SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_spring框架_03

在这里插入图片描述


PropertiesLoaderUtils.loadProperties


SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_插入图片_04

在这里插入图片描述


从源码可以看出也是支持xml文件读取的,能支持reader就获取reader对象,否则出件inputStream

SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_资源文件_05

在这里插入图片描述

SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_插入图片_06

在这里插入图片描述


load0方法是关键,这里加了同步锁


SpringBoot源码学习系列之@PropertySource不支持yaml读取原因_spring框架_07

在这里插入图片描述


很重要的load0 方法抓取出来:

private void load0 (LineReader lr) throws IOException {
char[] convtBuf = new char[1024];
int limit;
// 当前key所在位置
int keyLen;
// 当前value所在位置
int valueStart;
char c;//读取的字符
boolean hasSep;
boolean precedingBackslash;//是否转义字符,eg:/n etc.
// 一行一行地读取
while ((limit = lr.readLine()) >= 0) {
c = 0;
keyLen = 0;
valueStart = limit;
hasSep = false;

//System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
precedingBackslash = false;
//key的长度小于总的字符长度,那么就进入循环
while (keyLen < limit) {
c = lr.lineBuf[keyLen];
//need check if escaped.
if ((c == '=' || c == ':') && !precedingBackslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
} else if ((c == ' ' || c == '\t' || c == '\f') && !precedingBackslash) {
valueStart = keyLen + 1;
break;
}
if (c == '\\') {
precedingBackslash = !precedingBackslash;
} else {
precedingBackslash = false;
}
keyLen++;
}
//value的起始位置小于总的字符长度,那么就进入该循环
while (valueStart < limit) {
c = lr.lineBuf[valueStart];
//当前字符是否非空格类字符
if (c != ' ' && c != '\t' && c != '\f') {
if (!hasSep && (c == '=' || c == ':')) {
hasSep = true;
} else {
break;
}
}
valueStart++;
}
//读取key
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
//读取value
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, value);
}
}

ok,从源码可以看出,这个方法是一行一行地读取,然后根据冒号、等于号、空格等进行校验,经过一系列遍历之后获取key和value,而yaml语法是以缩进来辨别的,经过自己调试,这个方法也是不支持yaml文件的读取的,properties源码是比较多的,具体的Properties源码实现的可以参考博客

然后要支持的话,具体怎么实现,参考我博客:SpringBoot系列之@PropertySource读取yaml文件

举报

相关推荐

0 条评论