springboot如何通过@PropertySource加载自定义yml文件

2022-07-16,,,,

@propertysource加载自定义yml文件

使用@propertysource默认加载的是.xml或者 .properties文件,因为在注解源码默认使用的是defaultpropertysourcefactory实现处理文件内容,spring使用resourcepropertysource从resource构建properties传给spring。

系统的应用,比如加载自定义的文件,将配置文件内容存储在内存,如下:

那么加载一个自定义的.yml文件,就需要自定义实现resourcepropertysource来处理yml文件的类

public class yamlpropertysourcefactory implements propertysourcefactory {
    @override
    public propertysource<?> createpropertysource(string name, encodedresource resource) throws ioexception {
        properties propertiesfromyaml = loadyamlintoproperties(resource);
        string sourcename = name != null ? name : resource.getresource().getfilename();
        return new propertiespropertysource(sourcename, propertiesfromyaml);
    }
    private properties loadyamlintoproperties(encodedresource resource) throws filenotfoundexception {
        try {
            yamlpropertiesfactorybean factory = new yamlpropertiesfactorybean();
            factory.setresources(resource.getresource());
            factory.afterpropertiesset();
            return factory.getobject();
        } catch (illegalstateexception e) {
            // for ignoreresourcenotfound
            throwable cause = e.getcause();
            if (cause instanceof filenotfoundexception)
                throw (filenotfoundexception) e.getcause();
            throw e;
        }
    }
}

@propertysource注解对于yml的支持

@propertysource只对properties文件可以进行加载,但对于yml或者yaml不能支持。

追寻源码。

public class defaultpropertysourcefactory implements propertysourcefactory {
    public defaultpropertysourcefactory() {
    }
    public propertysource<?> createpropertysource(string name, encodedresource resource) throws ioexception {
        return name != null ? new resourcepropertysource(name, resource) : new resourcepropertysource(resource);
    }
}

我们只需要继承defaultpropertysourcefactory类并修改就可以了。

public class yamlconfigfactory extends defaultpropertysourcefactory {
    @override
    public propertysource<?> createpropertysource(string name, encodedresource resource) throws ioexception {
        string sourcename = name != null ? name : resource.getresource().getfilename();
        if (!resource.getresource().exists()) {
            return new propertiespropertysource(sourcename, new properties());
        } else if (sourcename.endswith(".yml") || sourcename.endswith(".yaml")) {
            properties propertiesfromyaml = loadyml(resource);
            return new propertiespropertysource(sourcename, propertiesfromyaml);
        } else {
            return super.createpropertysource(name, resource);
        }
    }
    private properties loadyml(encodedresource resource) throws ioexception {
        yamlpropertiesfactorybean factory = new yamlpropertiesfactorybean();
        factory.setresources(resource.getresource());
        factory.afterpropertiesset();
        return factory.getobject();
    }
}
@propertysource(value = {"classpath:dog.yml"},factory = yamlconfigfactory.class)
@component
@configurationproperties(prefix = "dog")
public class dog {
    private string name ;
    private string age ;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

《springboot如何通过@PropertySource加载自定义yml文件.doc》

下载本文的Word格式文档,以方便收藏与打印。