简体   繁体   English

Spring内部的war无法在内部jar文件中找到classpath资源

[英]Spring inside war cannot find classpath resource in an inner jar file

I have a project organized like this: 我有一个像这样组织的项目:

core
  -- /src/main/resources/company/config/spring-config.xml
webapp
  -- /WEB-INF/applicationContext.xml

The webapp depends on core.jar , which is included correctly in WEB-INF/lib when I deploy. webapp依赖于core.jar ,我在部署时正确地包含在WEB-INF/lib

In web.xml I have: web.xml我有:

<param-value>
    /WEB-INF/applicationContext.xml
</param-value>

And in applicationContext.xml I have: applicationContext.xml我有:

<import resource="classpath:/company/config/spring-config.xml" />

But when I run, I get this error: 但是当我跑步时,我收到了这个错误:

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

When spring-config.xml is in webapp, everything works fine. spring-config.xml在webapp中时,一切正常。

I noticed the leading / is removed from some of the errors on the stack trace, and I wonder if this has anything to do with it. 我注意到的领先/从一些堆栈跟踪误差的去除,我不知道这有什么用它做。

Also, I am (unfortunately) using Spring 2.5, if that matters. 另外,我(不幸的是)使用Spring 2.5,如果这很重要的话。

For future reference, I figured out the problem after much debugging. 为了将来参考,我经过多次调试后想出了问题。 It turns out Eclipse was building my "core" library as a jar, but with a web application package layout, so instead of my resource being located here: 事实证明Eclipse正在将我的“核心”库构建为jar,但是使用Web应用程序包布局,因此我的资源不在此处:

/company/config/spring-config.xml

it was located here: 它位于这里:

/WEB-INF/company/config/spring-config.xml

which caused the problem. 这引起了这个问题。 I had checked the jar a few times before, but had never noticed the sneaky "/WEB-INF" hiding in plain sight. 我曾经几次检查过这个罐子,但从来没有注意到偷偷摸摸的“/ WEB-INF”隐藏在视线之内。

Removing the project and re-importing it into Eclipse (via the Maven pom.xml file) was not enough to fix the problem. 删除项目并将其重新导入Eclipse(通过Maven pom.xml文件)还不足以解决问题。

I had to manually delete the Eclipse-specific .project, .classpath, and .settings files. 我不得不手动删除特定于Eclipse的.project,.classpath和.settings文件。 When I did that and re-imported the project everything was fixed. 当我这样做并重新导入项目时,一切都得到了修复。

The moral of the lesson is: ALWAYS check your resource paths when the exception says "File Not Found". 本课程的寓意是:当异常显示“找不到文件”时, 总是检查资源路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM