实际的问题是:是否有一种方法可以让XmlWebApplicationContext使用相对于上下文位置的路径来加载资源?为了清楚起见,让我们说“上下文位置”是通过setConfigLocation()方法指定的第一个文件的位置。
详细解释如下:
我在web层使用Spring,在中间层使用Spring。适当的上下文是按层次定义的,如春季文献中所描述的: web在my-servlet.xml中定义,服务等在通过ContextLoaderListener加载的services.xml中定义。中间层可以与web层一起部署(例如,整个任务在ServletContainer中运行),也可以单独部署(在这种情况下,services.xml被定义远程存根的remote-services.xml替换)。除以下问题外,整个设置工作得很完美:
我有某些资源(额外的XML文件,您有什么)位于与services.xml相同的文件夹中,这些文件夹需要由上述服务访问。这些资源在services.xml中使用相对路径指定为依赖项。当中间层单独部署时,工作正常,但在servlet容器中部署时就不行了。在后一种情况下,中间层上下文被实例化为XmlWebApplicationContext,它基于servlet上下文根加载所有资源,这意味着我必须用/WEB/作为前缀,这是我真正想要避免的。使用PropertyPlaceholderConfigurer也带来了类似的问题。
我知道我可以在某种程度上通过从类路径加载资源来解决这个问题,但这也不理想--对于独立部署来说,这意味着我需要向类路径添加配置文件夹,对于web部署,意味着所有东西都必须在WEB/类下复制。
有什么想法吗?
发布于 2009-07-17 19:13:36
最后,我扩展了Spring的XmlWebApplicationContext,以允许相对的资源路径。这可以做我想做的事,也就是允许我使用相同的context.xml文件,不管它是作为web应用程序的一部分部署,还是作为独立的应用程序部署。
对于任何感兴趣的来源,可以在下面找到。它是使用SOV (堆栈溢出投票)许可证发布的:-),这意味着您可以随意使用它,只要您投票这个答案:-)
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
/**
* Extends Spring's default web application context to allow relative
* resource paths. Resources without explicitly specified URL protocol
* and / or leading slash are loaded relative to the first location
* from getConfigLocations().
*/
public class SpringApplicationContext extends XmlWebApplicationContext {
@Override
protected Resource getResourceByPath(String path) {
path = StringUtils.cleanPath(path);
if (path.startsWith("/") || (path.indexOf(':')>0)) {
return super.getResourceByPath(path);
}
try {
return super.getResourceByPath(getConfigLocations()[0])
.createRelative(path);
} catch (IOException E) {
// failed to create relative resource - default to standard implementation
return super.getResourceByPath(path);
}
} // getResourceByPath()
}发布于 2009-07-14 09:40:47
我同意这是相当烦人的。我通过执行您建议的操作来解决这个问题,这就是将我的spring配置放在类路径上,所以即使我仍然使用完全合格的导入,它们在任何环境下都能工作。
不过,我不知道为什么您的类路径配置需要这么复杂。这些文件可以与java文件一起放在java源文件夹下,因此它们可以得到相同的处理。
发布于 2010-11-08 13:59:03
真奇怪。你的解决方案对我没用。这是我的:
package dmp.springframework.web.context;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class RelativeResourceXmlWebApplicationContext extends XmlWebApplicationContext {
@Override
protected Resource getResourceByPath(String path) {
path = StringUtils.cleanPath(path);
if (path.startsWith("/") || (path.contains(":"))) {
return super.getResourceByPath(path);
}
try {
String newFilename = super.getResourceByPath(getConfigLocations()[0]).getFile().getParentFile().getAbsolutePath();
newFilename = newFilename + "/" + path;
return new FileSystemResource(newFilename);
} catch (IOException E) {
// failed to create relative resource - default to standard implementation
return super.getResourceByPath(path);
}
} // getResourceByPath()
}https://stackoverflow.com/questions/1123335
复制相似问题