Rory's Blog
          Happy study,Happy work,Happy life
          posts - 22,  comments - 46,  trackbacks - 0
          在以前的項(xiàng)目中對(duì)于一些資源的配置基本上都是通過(guò)spring的IOC注入一個(gè)目錄的地址字符串。而這樣的問(wèn)題是,對(duì)于開發(fā)中的團(tuán)隊(duì)來(lái)說(shuō)還是很有問(wèn)題的,因?yàn)槊總€(gè)可能都配置一個(gè)不同的本地目錄,而發(fā)布到服務(wù)器之后又有不同的目錄。這樣造成每個(gè)人提交了配置文件之后其他人都可能需要修改配置文件才能正確啟動(dòng)服務(wù)。這確實(shí)很令人煩勞。
          ???? 最近看《Professional Java Development with the Spring Framework》時(shí)看到了spring對(duì)底層資源的抽象,才找到了完美解決方案。
          ???? 原來(lái)的代碼:
          ????private?String?templatePath;
          ????
          public?void?setTemplatePath(String?templatePath)?{
          ????????
          this.templatePath?=?templatePath;
          ????}
          ????
          public?void?initListener()?{
          ????????TemplateEventListener?templateListener?
          =?new?TemplateEventListener(){
          ????????????
          public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
          ????????????????
          //?添加事件到隊(duì)列中
          ????????????????queue.offer(evt);
          ????????????????
          if(log.isDebugEnabled()){
          ????????????????????log.debug(
          "Add?Template?about:"?+?evt.getTemplateName());
          ????????????????}
          ????????????}
          ????????????
          ????????};
          ????????
          ????????
          //注冊(cè)模版監(jiān)聽事件
          ????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
          ????????
          ????????
          ????????
          //設(shè)置freemarker的參數(shù)
          ????????freemarkerCfg?=?new?Configuration();
          ????????
          try?{
          ????????????freemarkerCfg.setDirectoryForTemplateLoading(
          new?File(templatePath));
          ????????????freemarkerCfg.setObjectWrapper(
          new?DefaultObjectWrapper());
          ????????????freemarkerCfg.setDefaultEncoding(
          "UTF-8");
          ????????}?
          catch?(IOException?ex)?{
          ????????????
          throw?new?SystemException("No?Directory?found,please?check?you?config.");
          ????????}
          ????}
          配置文件

          ????
          <bean?id="buildHtmlService"?class="cn.jdk.leaf.service.impl.BuildHtmlServiceImpl"?init-method="initListener">
          ????????
          <property?name="templatePath"><value>${templatePath}</value></property>
          ????
          </bean>
          templatePath.path=D:/template
          使用spring對(duì)底層資源的抽象只要把templatePath改成Resource就可以了
          ????private?Resource?templatePath;
          ????
          public?void?setTemplatePath(Resource?templatePath)?{
          ????????
          this.templatePath?=?templatePath;
          ????}
          ????
          public?void?initListener()?{
          ????????????TemplateEventListener?templateListener?
          =?new?TemplateEventListener(){
          ????????????
          public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
          ????????????????
          //?添加事件到隊(duì)列中
          ????????????????queue.offer(evt);
          ????????????????
          if(log.isDebugEnabled()){
          ????????????????????log.debug(
          "Add?Template?about:"?+?evt.getTemplateName());
          ????????????????}
          ????????????}
          ????????????
          ????????};????
          ????????
          //注冊(cè)模版監(jiān)聽事件
          ????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
          ????????
          ????????
          ????????
          //設(shè)置freemarker的參數(shù)
          ????????freemarkerCfg?=?new?Configuration();
          ????????
          try?{
          ????????????freemarkerCfg.setDirectoryForTemplateLoading(templatePath.getFile());
          ????????????freemarkerCfg.setObjectWrapper(
          new?DefaultObjectWrapper());
          ????????????freemarkerCfg.setDefaultEncoding(
          "UTF-8");
          ????????}?
          catch?(IOException?ex)?{
          ????????????
          throw?new?SystemException("No?Directory?found,please?check?you?config.");
          ????????}
          ????}
          bean的配置不變,只要修改properties文件就可以了。
          ????<bean?id="buildHtmlService"?class="cn.jdk.leaf.service.impl.BuildHtmlServiceImpl"?init-method="initListener">
          ????????
          <property?name="templatePath"><value>${templatePath}</value></property>
          ????
          </bean>
          把properties文件修改成
          templatePath.path=template
          在webcontext目錄下面建立一個(gè)template目錄就可以了。在部署到服務(wù)器的時(shí)候需要部署到一個(gè)特定的目錄只要修改這個(gè)配置文件為
          templatePath.path=file:/D:/template
          這樣就可以了。

          創(chuàng)造共用協(xié)議:署名,非商業(yè),保持一致??除經(jīng)特別注明外,本文章版權(quán)歸莫多泡泡所有.
          署名,非商業(yè)用途,保持一致.???somebody(莫多)???
          posted on 2006-06-11 23:01 莫多 閱讀(1866) 評(píng)論(2)  編輯  收藏 所屬分類: Spring

          FeedBack:
          # re: spring關(guān)于底層資源的抽象是如此方便
          2006-06-12 08:28 | Andy luo
          在properties文件里修改跟在配置文件里修改不是一樣麻煩嗎?  回復(fù)  更多評(píng)論
            
          # re: spring關(guān)于底層資源的抽象是如此方便
          2006-06-13 09:12 | lizongbo
          @Andy luo
          只在使用絕對(duì)路徑的時(shí)候才需要修改。

          Resource 的詳解可以參考:
          http://gocom.primeton.com/blog93_23.htm?PHPSESSID=824fb401cebfa7b51b96b16df2342849


            回復(fù)  更多評(píng)論
            

          <2006年6月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(1)

          隨筆分類(27)

          隨筆檔案(22)

          Friends

          搜索

          •  

          積分與排名

          • 積分 - 62245
          • 排名 - 845

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 绥芬河市| 金门县| 京山县| 海林市| 菏泽市| 山东省| 清水河县| 屏东县| 正宁县| 慈利县| 菏泽市| 琼海市| 津市市| 瑞金市| 体育| 商洛市| 县级市| 会昌县| 麻江县| 大安市| 苍梧县| 洪洞县| 团风县| 万源市| 明星| 和平区| 中牟县| 宁明县| 高尔夫| 馆陶县| 延寿县| 望城县| 苍梧县| 扶沟县| 綦江县| 老河口市| 上蔡县| 鹤峰县| 政和县| 固安县| 东辽县|