我有一个项目是这样组织的:
core
-- /src/main/resources/company/config/spring-config.xml
webapp
-- /WEB-INF/applicationContext.xml
Web 应用程序取决于 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
在 Web 应用程序中时,一切正常。
我注意到前导/
已从堆栈跟踪上的一些错误中删除,我想知道这是否与此有关。
另外,如果这很重要,我(不幸的是)正在使用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 文件。当我这样做并重新导入项目时,一切都已修复。
该课程的寓意是:当异常显示"找不到文件"时,请始终检查您的资源路径。