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

时间:2019-11-26
本文章向大家介绍SpringBoot源码学习系列之@PropertySource不支持yaml读取原因,主要包括SpringBoot源码学习系列之@PropertySource不支持yaml读取原因使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

@PropertySource源码:

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

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

PropertiesLoaderUtils.loadProperties

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


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

很重要的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源码实现的可以参考博客:https://www.cnblogs.com/liuming1992/p/4360310.html,这篇博客写的比较详细

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

原文地址:https://www.cnblogs.com/mzq123/p/11936369.html