spring boot 读取外部配置文件

 首先在resources目录下新增META-INF文件夹,然后新建spring.factories文件,文件内容为:

org.springframework.boot.env.EnvironmentPostProcessor=com.ex.gays.framework.config.LocalSettingsEnvironmentPostProcessor

以上为我的配置

key值为:org.springframework.boot.env.EnvironmentPostProcessor

value值为你项目中读取外部文件配置类的package path

下面就是我的读取外部文件配置类的详细代码,学习的公司大佬(*^__^*) 

(读取配置有个开关判断,是一个常量值,就不做代码说明了)

package com.ex.gays.framework.config;

import com.ex.gays.common.utils.ConstParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

@Slf4j
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor {

    // 本地测试路径
    private static final String LOCATION_CONF_PATH = "D:\\JavaOther\\apache-tomcat-8.0.52\\myconf\\";
    // 外部配置文件
    private static final String CONF_NAME = "application.yml";
    // tomcat启动参数key
    private static final String SYSTEM_CONF_PATH_KEY = "spring.application.conf.path";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
        // 判断开关
        if (ConstParams.PROPERTY_SWITCH.equals("on")) {
            String systemPropPath = System.getProperty(SYSTEM_CONF_PATH_KEY);
            log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> systemPropPath : {}" ,systemPropPath);
            if (StringUtils.isEmpty(systemPropPath)) {
                log.error("SystemPropPath is null or empty, Application run failed!");
                throw new RuntimeException("Failed to load system property from spring.application.conf.path ! ");
            }
            String fullPath = systemPropPath.concat(File.separator).concat(CONF_NAME);
            log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> fullPath : {}" ,fullPath);

            try {
                File file = new File(fullPath);
                if (file.exists()) {
                    MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
                    Properties properties = loadProperties(file);
                    log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> properties toString : {}", properties.toString());
                    propertySources.addFirst(new PropertiesPropertySource("Config", properties));
                }
            } catch (Exception e) {
                log.error("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment , Load system property exception , Please check configuration", e);
            }
        }
    }

    private Properties loadProperties(File file) {
        FileSystemResource resource = new FileSystemResource(file);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to load file resource from " + file.getAbsolutePath(), e);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值