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類的源碼
- package?org.expframework;???
- ???
- import?java.io.IOException;???
- import?java.io.InputStream;???
- import?java.util.Properties;???
- ???
- /**??
- ?*?Contains?the?configuration?information?of?application.??
- ?*???
- ?*?@author?ShriKant??
- ?*?@version?1.0??
- ?*/???
- public?class?AppConfig?{???
- ???
- ????/**??
- ?????*?Instance?of?AppConfig??
- ?????*/???
- ????private?static?final?AppConfig?config?=?new?AppConfig();???
- ???
- ????/**??
- ?????*?Contains?all?<CODE>properties</CODE>?in?a?Properties?instance??
- ?????*/???
- ????private?Properties?properties;???
- ???
- ????/**??
- ?????*?Creates?an?AppConfig?instance.??
- ?????*/???
- ????private?AppConfig()?{???
- ????????properties?=?new?Properties();???
- ???
- ????????InputStream?stream?=?AppConfig.class???
- ????????????????.getResourceAsStream("/app.properties");???
- ???
- ????????if?(stream?==?null)?{???
- ????????????throw?new?IllegalArgumentException(???
- ????????????????????"Could?not?load?app.properties.?Please?make?sure?that?it?is?in?CLASSPATH.");???
- ????????}???
- ???
- ????????try?{???
- ????????????properties.load(stream);???
- ????????}?catch?(IOException?e)?{???
- ????????????IllegalStateException?ex?=?new?IllegalStateException(???
- ????????????????????"An?error?occurred?when?reading?from?the?input?stream");???
- ????????????ex.initCause(e);???
- ????????????throw?ex;???
- ????????}?finally?{???
- ????????????try?{???
- ????????????????stream.close();???
- ????????????}?catch?(IOException?e)?{???
- ????????????????IllegalStateException?ex?=?new?IllegalStateException(???
- ????????????????????????"An?I/O?error?occured?while?closing?the?stream");???
- ????????????????ex.initCause(e);???
- ????????????????throw?ex;???
- ????????????}???
- ????????}???
- ????}???
- ???
- ????/**??
- ?????*?Gets?the?instance?of?<CODE>AppConfig</CODE>.??
- ?????*???
- ?????*?@return?instance?of?AppConfig??
- ?????*/???
- ????public?static?AppConfig?getInstance()?{???
- ????????return?config;???
- ????}???
- ???
- ????/**??
- ?????*?Gets?the?value?of?a?property?based?on?key?provided.??
- ?????*???
- ?????*?@param?key??
- ?????*????????????The?key?for?which?value?needs?to?be?retrieved?from??
- ?????*?@return?The?value?for?the?key?in?<CODE>app.properties</CODE>.?Returns??
- ?????*?????????null?if?the?value?does?not?exist?in?the?property?file.??
- ?????*????
- ?????*/???
- ????public?String?get(String?key)?{???
- ????????if?(properties?==?null?||?key?==?null)???
- ????????????return?null;???
- ????????return?(String)?properties.get(key);???
- ????}???
- }??
posted on 2009-07-25 22:19 tobyxiong 閱讀(2265) 評論(0) 編輯 收藏 所屬分類: java