这里的配置文件都放在src下面, System.properties的内容
exceptionMapping=exceptionMapping.properties
config=config.properties
sys.core=systemCore.properties
sys.boss=bossPort.properties
bossPort.properties的内容
#查询机场火车站sim卡剩余次数的服务
NgCallServiceURL=http://*.*.*6.*:5***0/esbWS/services/NgCallService
#用户基本信息
sQUserBaseURL=http://*.*.*6.*:5***0/esbWS/services/sQUserBaseL
#用户积分查询
S3979SrcQryURL=http://*.*.*6.*:5***0/esbWS/services/s3979SrcQry
读取properties配置文件的java类
import java.io.*;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class GlobalConfig { private static Log log;
// system.properties是最根的配置文件,并且在src下面
public static final String SYSTEM_PROPERTIES = "/system.properties"; private static String propertiesStorePath;
private static Map propertieMap;
private static Map propertieFileMap; static {
log = LogFactory.getLog(com.sinovatech.common.config.GlobalConfig.class);
propertieMap = new HashMap();
propertieFileMap = new HashMap();
Properties properties = init("/system.properties");
Iterator it = properties.keySet().iterator();
propertiesStorePath = properties.getProperty("path");
String name;
Properties p;
for (; it.hasNext(); propertieMap.put(name, p)) {
name = (String) it.next();
String file = properties.getProperty(name);
file = file.trim();
System.out.println();
System.out.println("name "+name+" file "+file);
System.out.println();
propertieFileMap.put(name, file);
p = init("/" + file);
} } public GlobalConfig() {
} private static Properties init(String propertyFile) {
Properties p = new Properties();
try {
log.info("Start Loading property file \t" + propertyFile);
System.out.println("Start Loading property file \t" + propertyFile);
p.load(com.sinovatech.common.config.GlobalConfig.class
.getResourceAsStream(propertyFile));
log.info("Load property file success!\t" + propertyFile);
System.out.println("Load property file success!\t" + propertyFile);
} catch (Exception e) {
e.printStackTrace();
log.error("Could not load property file." + propertyFile, e);
}
return p;
} public static String getProperty(String cls, String name) {
Properties p = (Properties) propertieMap.get(cls);
if (p != null)
return p.getProperty(name);
else
return null;
} public static boolean getBooleanProperty(String cls, String name) {
String p = getProperty(cls, name);
return "true".equals(p);
} public static Integer getIntegerProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Integer.valueOf(p);
} public static Long getLongProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Long.valueOf(p);
} public static Double getDoubleProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Double.valueOf(p);
} public static void store() {
} public static void store(String cls) {
Properties p = (Properties) propertieMap.get(cls);
try {
FileOutputStream fi = new FileOutputStream(new File(
(String) propertieFileMap.get(cls)));
p.store(fi, "Modified time: " + Calendar.getInstance().getTime());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
测试GlobalConfig.java读取配置文件信息
public static void main(String[] args) {
String requestUrl=GlobalConfig.getProperty("sys.boss","S3979SrcQryURL");
System.out.println("\n"+requestUrl);
}
测试内容输入:
Start Loading property file /system.properties
Load property file success! /system.properties
name sys.core file systemCore.properties
Start Loading property file /systemCore.properties
Load property file success! /systemCore.properties
name exceptionMapping file exceptionMapping.properties
Start Loading property file /exceptionMapping.properties
Load property file success! /exceptionMapping.properties
name sys.boss file bossPort.properties
Start Loading property file /bossPort.properties
Load property file success! /bossPort.properties
name config file config.properties
Start Loading property file /config.properties
Load property file success! /config.properties
http://*.*.*6.*:5***0/esbWS/services/s3979SrcQry