如何优雅地加载一个properties配置文件
2014-12-04 17:05
148 查看
一、前言许多时候,系统都需要一个配置文件,重复编写这些配置文件读取程序很烦恼,又浪费时间 ,下面给一个通用的方法
二、方法论
好吧!我们用一个简单的单例模式来解决这个问题,一般来说配置文件都是加载一次就够了,最简单的单例模式(实践比空谈好)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().);}}
相关文章推荐
- Web工程和引用工程的CLASSPATH下存在同一个配置文件,例如test.properties,部署后会加载哪个?
- spring 加载配置文件application.properties,类里如何调用@Value的解决办法
- <util:properties/>加载的配置文件中有中文导致乱码,如何通过分析源码解决问题?
- (1) 写一个 Properties 格式的配置文件,配置类的完整名称。 * (2) 写一个程序,读取这个 Properties 配置文件,获得类的完整名称并加载这个类,用 反射 的方式运行
- 如何使用java类来加载properties配置文件的属性信息
- spring加载一个或者多个properties配置文件方法
- 当我们修改WEB-INF下的任何一个配置文件的时候,如何让tomcat自动加载
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- java如何从properties配置文件里面读数据?
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- phpmyadmin配置(无法加载 mcrypt扩展,请检查您的 PHP 配置和配置文件现在需要一个短语密码)
- 如何获取properties后缀配置文件中的键值对
- PHP如何解析一个ini的配置文件?
- 一个比较全的的log4j配置文件(log4j.properties)转载
- 如何在spring中读取properties配置文件里面的信息
- 一个log4j.properties配置(每天一个日志文件)
- struts2如何加载配置文件(总结)
- 在前台页面修改properties配置文件后 tomcat重新加载该配置文件的问题
- 64位系统 IIS7.0配置.net网站时报错:未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
- Android配置文件Manifest.xml如何被加载、解析的