我相信您需要指定绝对路径而不是 Web 应用程序相对路径FileSystemResource http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/io/FileSystemResource.html.
尝试使用ServletContextResource http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/support/ServletContextResource.html反而。
Resource http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/io/Resource.html实施ServletContext http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html?is-external=true资源,
解释内的相对路径
Web 应用程序根目录。
唯一的问题是你需要ServletContext http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html?is-external=true so:
ServletContext servletContext = ...
Resource res = new ServletContextResource(servletContext,
"/WEB-INF/businesscaliber-servlet.xml");
BeanFactory factory = new XmlBeanFactory(res);
if (factory != null && beanId != null) {
obj = factory.getBean(beanId);
}
值得注意的是,理想情况下您可以从ApplicationContext http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationContext.html. From 4.4 资源加载器 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-resourceloader of the 弹簧参考 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt);
返回的将是ClassPathResource
;如果相同
方法是针对 a 执行的FileSystemXmlApplicationContext
例如,你会得到一个FileSystemResource
。为一个WebApplicationContext
,你会得到
回一个ServletContextResource
, 和
很快。
因此,您可以将资源加载到
适合特定人群的时尚
应用程序上下文。
所以这是检索资源的首选方法。
或者因为/WEB-INF/
从技术上讲,您可以使用类路径classpath:
前缀(根据您的评论)或使用ClassPathXmlApplicationContext http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html(这将自动返回类路径资源)。
另外,也没有必要添加双正斜杠。不知道为什么要这样做。也许是双反斜杠的保留,这是必要的?