1. 程式人生 > >基於ConfigManager讀取配置檔案的資料

基於ConfigManager讀取配置檔案的資料

獲取配置檔案的配置資訊(比如資料庫的配置資訊,redis的配置資訊)
如何讓使用者只能建立一個ConfigManager?單例模式:(1)把構造方法私有  (2)程式提供給別人唯一物件
單例模式的兩種實現方式:餓漢方式    懶漢方式(執行緒不安全) 
提供給別人一個唯一的ConfigManager物件,用static,使用時用 類名.方法 就行,不需要new物件,因為new的是私有化private的構造方法
懶漢方式 加一個鎖synchronized,只有當第一個人用完後第二個人才能用,提高執行緒安全

檔名前不加“/”,則表示從當前類所在的包下查詢該資源。

檔案前面加“/”,則表示從當前類所在的包下查詢該資源。

程式碼如下:

package com.ifeng.auto.api.util;

import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class ConfigManager {

   private static final Logger log = new Logger();
   private static ConfigManager configManger = new ConfigManager();

   private static final String defaultFileName = "auto";
   
   private volatile static Map<String, Properties> proMap = new ConcurrentHashMap<String, Properties>();

   private ConfigManager() {

   }

   public static ConfigManager instance() {
      return configManger;
   }

   /**
    * 
    * @param profileName
    *            配置檔名(不包括字尾)
    * @param key
    *            屬性key值
    * @return
    */
   public String getProperty(String profileName, String key) {
      if (key == null || profileName == null) {
         throw new IllegalArgumentException("key is null");
      }
      Properties properties = proMap.get(profileName);

      if (properties == null) {
         synchronized (this) {

            if (properties == null) {

               properties = this.loadProps(profileName);

               if (properties != null) {
                  proMap.put(profileName, properties);
               } else {
                  return null;
               }
            }
         }
      }

      String value = properties.getProperty(key);
      return value;
   }
   public void clearProps(String proFileName) {
      proMap.remove(proFileName);
   }
   public String getProperty(String key) {
      return getProperty(defaultFileName,key);
   }
   /**
    * 
    * @param profileName
    *            配置檔名(不包括字尾)
    * @param key
    *            屬性key值
    * @return
    */
   public int getIntProperty(String profileName, String key) {
      if (key == null || profileName == null) {
         throw new IllegalArgumentException("key is null");
      }

      String intValue = this.getProperty(profileName, key);

      try {
         return Integer.parseInt(intValue);
      } catch (Exception e) {
         //e.printStackTrace();
         log.error(profileName+"."+key+"="+intValue + " has exeception:"+e.toString());
         return -1;
      }
   }
   public int getIntProperty(String key) {
      return getIntProperty(defaultFileName,key);
   }

   public Properties loadProps(String proFileName) {

      log.debug("Getting Config");

      Properties properties = null;

      InputStream in = null;

      try {
         properties = new Properties();
         if (proFileName != null && proFileName.startsWith("http:")) {
            URL url = new URL(proFileName);
            in = url.openStream();
         } else {
            String fileName = "/" + proFileName + ".properties";
            in = getClass().getResourceAsStream(fileName);
            properties.load(in);
         }
         properties.load(in);
         log.info("Successfully  proFileName=" + proFileName + " load Properties:" + properties);

      } catch (Exception e) {
         e.printStackTrace();
         log.error("Error reading " + proFileName + " in loadProps() " + e.getMessage());
         log.error("Ensure the " + proFileName + " file is readable and in your classpath.");
      } finally {
         try {
            if (in != null)
               in.close();
         } catch (Exception e) {
            e.printStackTrace();
         }
      }

      return properties;
   }
}