在以前的项目中对于一些资源的配置基本上都是通过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.");
}
}
配置文件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文件就可以了。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 id="buildhtmlservice" class="cn.jdk.leaf.service.impl.buildhtmlserviceimpl" init-method="initlistener">
<property name="templatepath"><value>${templatepath}</value></property>
</bean>
把properties文件修改成<property name="templatepath"><value>${templatepath}</value></property>
</bean>
templatepath.path=template
在webcontext目录下面建立一个template目录就可以了。在部署到服务器的时候需要部署到一个特定的目录只要修改这个配置文件为templatepath.path=file:/d:/template
这样就可以了。
闽公网安备 35060202000074号