栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于springboot实现自定义配置文件外部挂载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于springboot实现自定义配置文件外部挂载

基于springboot实现自定义配置文件外部挂载 背景
spring支持载入jar包外的配置,但是仅仅只对application.properties主配置文件支持。这问题就大了,正式开发肯定不会把所有配置都写在主配置文件中。
理论

@PropertySource注解有一个factory参数,默认值是PropertySourceFactory.class,默认实现是DefaultPropertySourceFactory.class,这个参数就是用来指定配置文件加载工厂类的。
默认的实现是从jar包内查找配置文件,同时spring还有自带的外部文件资源类FileSystemResource,那要做的事情就很明显了。
spring默认的不是从jar包内查找吗,那我不变spring的原逻辑,我在它的基础上再加一层。不仅从jar内查找,额外从jar包外查找。规则如下:

jar包内有,jar包外没有,那自然是用jar包内的配置。
jar包内没有,jar包外有,自然是用jar包外的配置。
jar包内有,jar包外也有,使用jar包外的配置。
注意:这里是替换关系,不是补充关系。
编码 TomPropertySourceFactory 自定义配置文件载入工厂类
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;


public class TomPropertySourceFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource createPropertySource(String name, EncodedResource encodedResource) throws IOException {
        Resource fileSystemResource = null;
        // 判断jar包类有没有找到配置
        if (encodedResource.getResource() instanceof ClassPathResource) {
            String path = ((ClassPathResource) encodedResource.getResource()).getPath();
            fileSystemResource = new FileSystemResource(path);
        }
        // 如果外部配置也存在,则替换掉jar包内的配置
        if (fileSystemResource != null && fileSystemResource.exists()) {
            encodedResource = new EncodedResource(fileSystemResource, encodedResource.getEncoding());
        }
        // 调用原spring逻辑
        return super.createPropertySource(name, encodedResource);
    }
}
extra-config.properties 配置文件
extra.name=tomshidi
extra.properties.age=1000
extra.properties.sex='M'
ExtraConfig 配置类
import com.example.demo.factory.TomPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@PropertySource(value = "classpath:config/extra-config.properties",
        encoding = "UTF-8",
        factory = TomPropertySourceFactory.class)
@ConfigurationProperties(prefix = "extra")
public class ExtraConfig {
    private String name;

    private Map properties;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map getProperties() {
        return properties;
    }

    public void setProperties(Map properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "ExtraConfig{" +
                "name='" + name + ''' +
                ", properties=" + properties +
                '}';
    }
}
运行效果

载入外部文件 目录结构如下


配置文件内容
extra.name='This is an extra properties'
extra.properties.age=1000
extra.properties.sex='M'
cmd启动jar的效果

在容器部署时以上操作挺好用的
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764202.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号