专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

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

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

@PropertySource源码:
67_1.png
根据注释,默认使用DefaultPropertySourceFactory类作为资源文件加载类
67_2.png
里面还是调用Spring框架底层的PropertiesLoaderUtils工具类进行读取的
67_3.png
PropertiesLoaderUtils.loadProperties
67_4.png
从源码可以看出也是支持xml文件读取的,能支持reader就获取reader对象,否则出件inputStream
67_5.png

67_6.png
load0方法是关键,这里加了同步锁
67_7.png
很重要的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://tech.souyunku.com/liuming1992/p/4360310.html,这篇博客写的比较详细

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

未经允许不得转载:搜云库技术团队 » SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们