编程资料集中营
 | 网站首页 | 文章中心 | 编程资料2 | 软件下载 | BT下载 | 八卦星闻 | 音乐在线 | 在线游戏 | 免费电影 | 进入问吧 | 
spring关于底层资源的抽象,在以前的项目中对于一些资源的配置基本上都是通过spring的IOC注入一个目录的地址字符串。而这样的问题是,对于开发中的团队来说还是很有问题的,因为每个可能都配置一个不同的本地目录,而发布到服务器之后又有不同的目录。这样造成每个人提交了配置文件之后其他人都可能需要修改配置文件才能正确启动服务。这确实很令人烦劳。最近看《ProfessionalJavaDevelopmentwiththeSpringFramework》时看到了spring对底层资源的抽象,才找到了完美解决方案。原来的代码:privateStringtemplatePath;publicvoidsetTemplatePath(St,
您现在的位置: 编程资料,学习资料,c,c++,vc,vc++,java,jsp,j2ee,j2me,asp,php >> 文章中心 >> JAVA 专区 >> Spring 技术 >> 文章正文
【字体:
spring关于底层资源的抽象   进入问吧

本站地址:http://www.bajiao123.com

作者:佚名    文章来源:不详    点击数:    更新时间:2007-2-28    

spring关于底层资源的抽象

在以前的项目中对于一些资源的配置基本上都是通过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这样就可以了。
   

进入问吧

本站地址:http://www.bajiao123.com

文章录入:admin    责任编辑:admin 
高级搜索
编程资料集中营