java如何指定外部的配置文件

工作当中很多时候都希望可以把配置文件外放,这样的话就可以做到配置与业务分离,其实有很多种放式,比如xml,properties,这里就说一下如何用properties做到配置文件跟业务代码的分离


import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * Created by shengjk1 on 2017/12/11
 */
public class ConfigManager {
    protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(ConfigManager.class);

    private boolean autoReload;

    private static final String BASE_PATH;

    private static final String GLOBAL_CONFIG_PATH = "global.config.path";

    private static final String fileName="conf.properties";

    private static volatile PropertiesConfiguration prop =null;

    static{
        logger.info("开始初始化ConfigurationManager===================== ");
        SystemConfiguration sysConfig = new SystemConfiguration();
        String globalPath = sysConfig.getString(GLOBAL_CONFIG_PATH);
        logger.info("globalPath=======================  "+globalPath);
        if(StringUtils.isBlank(globalPath)){/**默认加载classpath下面的文件**/
            globalPath = Thread.currentThread().getContextClassLoader().getResource("conf.properties").getFile();
        }
        BASE_PATH=globalPath;
    }

    public ConfigManager(boolean autoReload) throws ConfigurationException {
        this.autoReload=autoReload;
        loadConfig();
    }

    public void loadConfig() throws ConfigurationException {
        if (null==prop){
            prop=new PropertiesConfiguration();
        }
        File file=new File(BASE_PATH);
        prop.setFile(file);
        prop.setAutoSave(false);
        if(autoReload){/**重载策略,5秒钟监视文件变化***/
            prop.setReloadingStrategy(new FileChangedReloadingStrategy());
        }
        prop.load();


    }


    /**
     * @param key
     * @return value
     */
    public  String getProperty(String key) {
        return prop.getString(key);
    }

    /**
     * 获取整数类型的配置项
     *
     * @param key
     * @return value
     */
    public  Integer getInteger(String key) {
        String value = getProperty(key);
        return Integer.valueOf(value);
    }

    /**
     * 获取布尔类型的配置项
     *
     * @param key
     * @return value
     */
    public  Boolean getBoolean(String key) {
        String value = getProperty(key);
        return Boolean.valueOf(value);
    }

    /**
     * 获取Long类型的配置项
     *
     * @param key
     * @return
     */
    public  Long getLong(String key) {
        String value = getProperty(key);
        return Long.valueOf(value);
    }


    private static class SingletonHelp {
        static ConfigManager instance;
        static {
            try {
                instance = new ConfigManager(true);
            } catch (ConfigurationException e) {
                logger.error("ConfigurationManager error" +e);
            }
        }
    }


    public static ConfigManager build(){
        return SingletonHelp.instance;
    }

}

当执行jar包的时候 java -jar -Dglobal.config.path=”配置文件位置”,即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shengjk1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值