我的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 閱讀(2265) 評論(0)  編輯  收藏 所屬分類: java

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 洪湖市| 洪泽县| 宁晋县| 景谷| 新河县| 长岛县| 海盐县| 丽水市| 商都县| 浪卡子县| 金寨县| 赣州市| 永清县| 紫金县| 额济纳旗| 乐东| 信丰县| 大荔县| 舒兰市| 新化县| 称多县| 武平县| 红原县| 万全县| 怀化市| 大姚县| 北宁市| 双鸭山市| 镇雄县| 阿拉尔市| 杭锦旗| 兰西县| 忻州市| 建平县| 天镇县| 黔西| 嵩明县| 利川市| 微山县| 桦川县| 张北县|