我的java天地

AppConfig此类解释

       今天看到项目中有很多AppConfig.getInstance().get("字符串xxx")。对于这个比较疑惑,后来知道。原来是项目中添加了第三方jar包expframework.jar
      
具体应用查看了下源码,源码地址http://www.pudn.com/downloads89/sourcecode/java/detail339948.html
在类路径下加入app.properties这个文件,文件中的内容以KEY  =   VALUE的形式保存,然后在类中以AppConfig.getInstance().get(key)。的形式得到值。其实就是对properties类作了一下封装面已
       以下是AppConfig类的源码

  1. package org.expframework;   
  2.    
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.util.Properties;   
  6.    
  7. /**  
  8.  * Contains the configuration information of application.  
  9.  *   
  10.  * @author ShriKant  
  11.  * @version 1.0  
  12.  */   
  13. public class AppConfig {   
  14.    
  15.     /**  
  16.      * Instance of AppConfig  
  17.      */   
  18.     private static final AppConfig config = new AppConfig();   
  19.    
  20.     /**  
  21.      * Contains all <CODE>properties</CODE> in a Properties instance  
  22.      */   
  23.     private Properties properties;   
  24.    
  25.     /**  
  26.      * Creates an AppConfig instance.  
  27.      */   
  28.     private AppConfig() {   
  29.         properties = new Properties();   
  30.    
  31.         InputStream stream = AppConfig.class   
  32.                 .getResourceAsStream("/app.properties");   
  33.    
  34.         if (stream == null) {   
  35.             throw new IllegalArgumentException(   
  36.                     "Could not load app.properties. Please make sure that it is in CLASSPATH.");   
  37.         }   
  38.    
  39.         try {   
  40.             properties.load(stream);   
  41.         } catch (IOException e) {   
  42.             IllegalStateException ex = new IllegalStateException(   
  43.                     "An error occurred when reading from the input stream");   
  44.             ex.initCause(e);   
  45.             throw ex;   
  46.         } finally {   
  47.             try {   
  48.                 stream.close();   
  49.             } catch (IOException e) {   
  50.                 IllegalStateException ex = new IllegalStateException(   
  51.                         "An I/O error occured while closing the stream");   
  52.                 ex.initCause(e);   
  53.                 throw ex;   
  54.             }   
  55.         }   
  56.     }   
  57.    
  58.     /**  
  59.      * Gets the instance of <CODE>AppConfig</CODE>.  
  60.      *   
  61.      * @return instance of AppConfig  
  62.      */   
  63.     public static AppConfig getInstance() {   
  64.         return config;   
  65.     }   
  66.    
  67.     /**  
  68.      * Gets the value of a property based on key provided.  
  69.      *   
  70.      * @param key  
  71.      *            The key for which value needs to be retrieved from  
  72.      * @return The value for the key in <CODE>app.properties</CODE>. Returns  
  73.      *         null if the value does not exist in the property file.  
  74.      *    
  75.      */   
  76.     public String get(String key) {   
  77.         if (properties == null || key == null)   
  78.             return null;   
  79.         return (String) properties.get(key);   
  80.     }   
  81. }  

posted on 2009-07-25 22:19 tobyxiong 阅读(2244) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(3)

随笔分类(144)

随笔档案(157)

相册

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜