package com.pds.WebConfig;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class ConfigManager {
public static void main(String[] args){
String a= ConfigManager.getParameter("tokenUrl");
System.out.print("aaa=="+a);
}
public static final String PROPERTY_FILE = "zhiyuanxml/zhiyuanconfig.properties";
private static Properties props;
private static ConfigManager instance;
static{
getInstance();
}
/**
* 私有的构造函数
*/
private ConfigManager() {
try {
props = new Properties();
props.load(getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE));
} catch (IOException e) {
throw new RuntimeException("系统资源文件访问错误:"+e.getMessage());
}
}
/**
* 确定路径的用这个方法麻烦,底层是
* URL url = new URL(location);
return new UrlResource(url);
跟如上方法没有差别
*/
private void ConfigManagertest() {
props = new Properties();
String location = "classpath:/sharepoint/sharepoint.properties";
InputStream is = null;
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] resourceArr;
try {
resourceArr = patternResolver.getResources(location);
for (Resource resource : resourceArr) {
try {
is = resource.getInputStream();
props.load(new InputStreamReader(is, "UTF-8"));
} catch (IOException ex) {
System.out.println("Could not load properties from path:" + location + ", " + ex.getMessage());
} finally {
IOUtils.closeQuietly(is);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单例模式,获取ConfigManager的引用
* @return ConfigManager
*/
public static ConfigManager getInstance() {
if (instance == null) {
instance = new ConfigManager();
}
return instance;
}
/**
* 根据参数名获取系统参数值
* @param parameterName String
* @return String
*/
public static String getParameter(String parameterName) {
return props.getProperty(parameterName);
}
}