這裏的配置文件都放在src下面, System.properties的內容java
exceptionMapping=exceptionMapping.properties config=config.properties sys.core=systemCore.properties sys.boss=bossPort.properties
bossPort.properties的內容apache
#查詢機場火車站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類app
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讀取配置文件信息ide
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