欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot如何通過@PropertySource加載自定義yml文件

 更新時間:2022年03月25日 10:35:04   作者:mobile18600007978  
這篇文章主要介紹了springboot如何通過@PropertySource加載自定義yml文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@PropertySource加載自定義yml文件

使用@PropertySource默認加載的是.xml或者 .properties文件,因為在注解源碼默認使用的是DefaultPropertySourceFactory實現(xiàn)處理文件內(nèi)容,spring使用ResourcePropertySource從Resource構(gòu)建Properties傳給Spring。

系統(tǒng)的應(yīng)用,比如加載自定義的文件,將配置文件內(nèi)容存儲在內(nèi)存,如下:

那么加載一個自定義的.yml文件,就需要自定義實現(xiàn)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 ;

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論