[英]Spring inside war cannot find classpath resource in an inner jar file
我有一个像这样组织的项目:
core
-- /src/main/resources/company/config/spring-config.xml
webapp
-- /WEB-INF/applicationContext.xml
webapp依赖于core.jar
,我在部署时正确地包含在WEB-INF/lib
。
在web.xml
我有:
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
在applicationContext.xml
我有:
<import resource="classpath:/company/config/spring-config.xml" />
但是当我跑步时,我收到了这个错误:
2012-10-04 20:03:39,156 [localhost-startStop-1] springframework.web.context.ContextLoader ERROR: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/company/config/spring-config.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [company/config/spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
....
Caused by: java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 36 more
当spring-config.xml
在webapp中时,一切正常。
我注意到的领先/
从一些堆栈跟踪误差的去除,我不知道这有什么用它做。
另外,我(不幸的是)使用Spring 2.5,如果这很重要的话。
为了将来参考,我经过多次调试后想出了问题。 事实证明Eclipse正在将我的“核心”库构建为jar,但是使用Web应用程序包布局,因此我的资源不在此处:
/company/config/spring-config.xml
它位于这里:
/WEB-INF/company/config/spring-config.xml
这引起了这个问题。 我曾经几次检查过这个罐子,但从来没有注意到偷偷摸摸的“/ WEB-INF”隐藏在视线之内。
删除项目并将其重新导入Eclipse(通过Maven pom.xml文件)还不足以解决问题。
我不得不手动删除特定于Eclipse的.project,.classpath和.settings文件。 当我这样做并重新导入项目时,一切都得到了修复。
本课程的寓意是:当异常显示“找不到文件”时, 总是检查资源路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.