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 莫多 阅读(1833) 评论(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

搜索

  •  

积分与排名

  • 积分 - 61426
  • 排名 - 847

最新评论

阅读排行榜

评论排行榜