首页 > 学院 > 开发设计 > 正文

spring关于底层资源的抽象

2019-11-18 12:58:53
字体:
来源:转载
供稿:网友

在以前的项目中对于一些资源的配置基本上都是通过sPRing的IOC注入一个目录的地址字符串。而这样的问题是,对于开发中的团队来说还是很有问题的,因为每个可能都配置一个不同的本地目录,而发布到服务器之后又有不同的目录。这样造成每个人提交了配置文件之后其他人都可能需要修改配置文件才能正确启动服务。这确实很令人烦劳。
     最近看《Professional java Development with the Spring Framework》时看到了spring对底层资源的抽象,才找到了完美解决方案。
     原来的代码:

 

 


 

Word-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">    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.");
        }
    }
配置文件


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表