`
somebody
  • 浏览: 80045 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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
这样就可以了。

创造共用协议:署名,非商业,保持一致  除经特别注明外,本文章版权归莫多泡泡所有.
署名,非商业用途,保持一致.   somebody(莫多)   


莫多 2006-06-11 23:01 发表评论
分享到:
评论

相关推荐

    Spring 2.0 开发参考手册

    3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 4.3.1. ...

    spring chm文档

    3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 ...

    Spring中文帮助文档

    3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 ...

    Spring API

    3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 ...

    Spring.3.x企业应用开发实战(完整版).part2

    3.3.1 资源抽象接口 3.3.2 资源加载 3.4 BeanFactory和ApplicationContext 3.4.1 BeanFactory介绍 3.4.2 ApplicationContext介绍 3.4.3 父子容器 3.5 Bean的生命周期 3.5.1 BeanFactory中Bean的生命周期 3.5.2 ...

    Spring3.x企业应用开发实战(完整版) part1

    3.3.1 资源抽象接口 3.3.2 资源加载 3.4 BeanFactory和ApplicationContext 3.4.1 BeanFactory介绍 3.4.2 ApplicationContext介绍 3.4.3 父子容器 3.5 Bean的生命周期 3.5.1 BeanFactory中Bean的生命周期 3.5.2 ...

    SpringMailTest.zip

    Spring 提供了一个发送电子邮件的高级抽象层,它向用户屏蔽了底层邮件系统的一些细节,同时负责低层次的代表客户端的资源处理。所以用 Spring 来发送 email 会省事很多和让发邮件变的简单许多,可以让我们更专注邮件...

    teemo:一个Java EE企业级通用开发框架,提供底层抽象和常用功能

    teemoTeemo是一个Java EE企业级通用开发框架,提供底层抽象和常用功能,包括基础CRUD、分页、角色权限、用户管理、资源管理、部门管理、数据库监控、自定义菜单等功能,完全开源,没有任何限制,在项目代码趋于稳定...

    Spring Framework 5.3.6

    • 数据库事务的一般化抽象层,允许宣告式(Declarative)事务管理器,简化事务的划分使之与底层无关。 • 内建的针对JTA和 单个JDBC数据源的一般化策略,使 Spring 的事务支持不要求Java EE环境,这与一般的JTA或者...

    16 Resource的前生今世慕课专栏1

    背景自信和希望是青年的特权。——大仲马Spring 把其资源做了一个抽象,底层使用统一的资源访问接口来访问 Spring 的所有资源。也就是说,不管什么格式的文

    java面试题

    底层环境或硬件问题。 exception是指异常,可以通过代码来解决的问题。 forward和redirect的区别? 答:forward是转发,浏览器跳转后不显示新的地址。 redirect是重定向,浏览器跳转后显示新的地址。 对比之下...

Global site tag (gtag.js) - Google Analytics