Rory's Blog
          Happy study,Happy work,Happy life
          posts - 22,  comments - 46,  trackbacks - 0
          在以前的項目中對于一些資源的配置基本上都是通過spring的IOC注入一個目錄的地址字符串。而這樣的問題是,對于開發(fā)中的團隊來說還是很有問題的,因為每個可能都配置一個不同的本地目錄,而發(fā)布到服務(wù)器之后又有不同的目錄。這樣造成每個人提交了配置文件之后其他人都可能需要修改配置文件才能正確啟動服務(wù)。這確實很令人煩勞。
          ???? 最近看《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());
          ????????????????}
          ????????????}
          ????????????
          ????????};
          ????????
          ????????
          //注冊模版監(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對底層資源的抽象只要把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());
          ????????????????}
          ????????????}
          ????????????
          ????????};????
          ????????
          //注冊模版監(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目錄下面建立一個template目錄就可以了。在部署到服務(wù)器的時候需要部署到一個特定的目錄只要修改這個配置文件為
          templatePath.path=file:/D:/template
          這樣就可以了。

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

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

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


            回復(fù)  更多評論
            

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

          常用鏈接

          留言簿(1)

          隨筆分類(27)

          隨筆檔案(22)

          Friends

          搜索

          •  

          積分與排名

          • 積分 - 62469
          • 排名 - 845

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 嫩江县| 改则县| 天门市| 扶风县| 丘北县| 武穴市| 内黄县| 辛集市| 灵璧县| 金门县| 武城县| 夏河县| 玛曲县| 故城县| 邳州市| 建平县| 长泰县| 万全县| 阿鲁科尔沁旗| 香格里拉县| 靖边县| 元谋县| 武陟县| 苏尼特左旗| 余干县| 晋中市| 湛江市| 庆元县| 长汀县| 达日县| 兴化市| 赞皇县| 建阳市| 洛扎县| 邮箱| 永春县| 鹤壁市| 长寿区| 西林县| 吉隆县| 镇巴县|