单例模式读取配置文件

读取配置文件 
满足 Lazy load,多线程也是一个实例
public class PropertiesConfig {
	private static final Properties prop = new Properties();
	static class PropertiesConfigHolder {  
        static PropertiesConfig instance = new PropertiesConfig();       
    }       
    public static PropertiesConfig getInstance() { 
        return PropertiesConfigHolder.instance;       
    } 
	public  String getKey(String url,String keyName){
        try {
			InputStream in = this.getClass().getClassLoader().getResourceAsStream(url);
			prop.load(in);
		} catch (Exception e) {
			System.out.println(e.getMessage() + "PropertiesConfig.getInstance().getKey()参数错误");
		}
		return prop.getProperty(keyName).trim();
	}
}
 





你可能感兴趣的:(java)