如何优雅地加载一个properties配置文件

本文介绍了如何在项目中优雅地加载properties配置文件,通过详细的方法论和具体代码实现,帮助开发者更好地管理和使用配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



一、前言

许多时候,系统都需要一个配置文件,重复编写这些配置文件读取程序很烦恼,又浪费时间 ,下面给一个通用的方法

二、方法论

好吧!我们用一个简单的单例模式来解决这个问题,一般来说配置文件都是加载一次就够了,最简单的单例模式(实践比空谈好)
1、私有静态对象
private static Config cfg = null;


2、构造器私有化
    private Config() {
        properties = new Properties();
        InputStream is = null;
        try {
            is = Config.class.getResourceAsStream(path);
            properties.load(is);
        } catch (Exception exception) {
            System.out.println("Can't read the properties file. ");
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    }


3、公有静态创建方法
    public static Config getInstance() {
        if (cfg == null) {
            cfg = new Config();
        }
        return cfg;
    }
    public static Config getInstance(String path) {
        path =path;
        if (cfg == null) {
            cfg = new Config();
        }
        return cfg;
    }



三、代码

package common.utils;

import common.log.Log;

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

/**
 * @author chenhaipeng
 * @version 1.0
 * @date 2014/12/04 17:25
 */
public class Config {
    private Properties properties;

    private static Config cfg = null;
    private String path = "/config/config.properties";

    private final static String ERR_MSG = "从配置文件中不能取得传入参数的返回值:";

    private Config() {
        properties = new Properties();
        InputStream is = null;
        try {
            is = Config.class.getResourceAsStream(path);
            properties.load(is);
        } catch (Exception exception) {
            System.out.println("Can't read the properties file. ");
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    }

    public static Config getInstance() {
        if (cfg == null) {
            cfg = new Config();
        }
        return cfg;
    }
    public static Config getInstance(String path) {
        path =path;
        if (cfg == null) {
            cfg = new Config();
        }
        return cfg;
    }

    /**
     * Retun a value for certain key.
     *
     * @param key a certain key define in properties file.
     * @return value
     */
    public String getValue(String key) {
        if (!properties.containsKey(key))
            return null;
        String value = properties.getProperty(key);
        if (value == null) {
            Log.getLogger().error(ERR_MSG + ":" + key);
        }

        return value;
    }

    public static void main(String args[]) {
        System.out.println(Config.getInstance("/config/test.properties").getValue("DB.DRIVER"));
//		System.out.println(Thread.currentThread().getContextClassLoader().);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值