Rory's Blog
          Happy study,Happy work,Happy life
          posts - 22,  comments - 46,  trackbacks - 0
          在以前的項(xiàng)目中對(duì)于一些資源的配置基本上都是通過spring的IOC注入一個(gè)目錄的地址字符串。而這樣的問題是,對(duì)于開發(fā)中的團(tuán)隊(duì)來說還是很有問題的,因?yàn)槊總€(gè)可能都配置一個(gè)不同的本地目錄,而發(fā)布到服務(wù)器之后又有不同的目錄。這樣造成每個(gè)人提交了配置文件之后其他人都可能需要修改配置文件才能正確啟動(dòng)服務(wù)。這確實(shí)很令人煩勞。
          ???? 最近看《Professional Java Development with the Spring Framework》時(shí)看到了spring對(duì)底層資源的抽象,才找到了完美解決方案。
          ???? 原來的代碼:
          ????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 莫多 閱讀(1879) 評(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

          搜索

          •  

          積分與排名

          • 積分 - 62464
          • 排名 - 845

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 虹口区| 临潭县| 苏州市| 安泽县| 宜川县| 松溪县| 白玉县| 柳林县| 玉山县| 丹棱县| 定边县| 古交市| 滦平县| 宜兰市| 格尔木市| 安多县| 彭山县| 汝阳县| 聊城市| 德安县| 顺义区| 察隅县| 长沙市| 德州市| 乌拉特后旗| 玉环县| 会宁县| 深圳市| 准格尔旗| 南木林县| 分宜县| 花垣县| 青海省| 无锡市| 大渡口区| 比如县| 岳普湖县| 和硕县| 南华县| 吐鲁番市| 磐石市|