Rory's Blog
          Happy study,Happy work,Happy life
          posts - 22,  comments - 46,  trackbacks - 0
          在以前的項目中對于一些資源的配置基本上都是通過spring的IOC注入一個目錄的地址字符串。而這樣的問題是,對于開發中的團隊來說還是很有問題的,因為每個可能都配置一個不同的本地目錄,而發布到服務器之后又有不同的目錄。這樣造成每個人提交了配置文件之后其他人都可能需要修改配置文件才能正確啟動服務。這確實很令人煩勞。
          ???? 最近看《Professional Java Development with the Spring Framework》時看到了spring對底層資源的抽象,才找到了完美解決方案。
          ???? 原來的代碼:
          ????private?String?templatePath;
          ????
          public?void?setTemplatePath(String?templatePath)?{
          ????????
          this.templatePath?=?templatePath;
          ????}
          ????
          public?void?initListener()?{
          ????????TemplateEventListener?templateListener?
          =?new?TemplateEventListener(){
          ????????????
          public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
          ????????????????
          //?添加事件到隊列中
          ????????????????queue.offer(evt);
          ????????????????
          if(log.isDebugEnabled()){
          ????????????????????log.debug(
          "Add?Template?about:"?+?evt.getTemplateName());
          ????????????????}
          ????????????}
          ????????????
          ????????};
          ????????
          ????????
          //注冊模版監聽事件
          ????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
          ????????
          ????????
          ????????
          //設置freemarker的參數
          ????????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對底層資源的抽象只要把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)?{
          ????????????????
          //?添加事件到隊列中
          ????????????????queue.offer(evt);
          ????????????????
          if(log.isDebugEnabled()){
          ????????????????????log.debug(
          "Add?Template?about:"?+?evt.getTemplateName());
          ????????????????}
          ????????????}
          ????????????
          ????????};????
          ????????
          //注冊模版監聽事件
          ????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
          ????????
          ????????
          ????????
          //設置freemarker的參數
          ????????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目錄下面建立一個template目錄就可以了。在部署到服務器的時候需要部署到一個特定的目錄只要修改這個配置文件為
          templatePath.path=file:/D:/template
          這樣就可以了。

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

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

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


            回復  更多評論
            

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

          常用鏈接

          留言簿(1)

          隨筆分類(27)

          隨筆檔案(22)

          Friends

          搜索

          •  

          積分與排名

          • 積分 - 62241
          • 排名 - 845

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 深州市| 华宁县| 永平县| 密云县| 咸阳市| 宜兴市| 锦屏县| 青龙| 潜山县| 习水县| 平泉县| 苍南县| 彭泽县| 贵溪市| 辽宁省| 广汉市| 周宁县| 玉山县| 东光县| 青铜峡市| 平安县| 连州市| 绥滨县| 泽库县| 横峰县| 介休市| 菏泽市| 平凉市| 舞钢市| 玉门市| 安图县| 常山县| 平昌县| 灵璧县| 遵义县| 汉中市| 仁布县| 尼玛县| 惠来县| 南江县| 思南县|