2012-10-05 74 views
10

类路径的资源我有这样的组织的项目:弹簧内部战争找不到一个内部jar文件

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是webapp,一切工作正常。

我注意到领先的/已从堆栈跟踪中的一些错误中删除,我不知道这与它有什么关系。

此外,我(不幸)使用Spring 2.5,如果这很重要。

+1

删除/从/company/config/spring-config.xml,然后检查 – Satya

+0

@Satya:谢谢,我试过了,我得到了同样的结果。 – JBCP

+0

@JBCP - 当你删除前导斜杠时,它可能不起作用,因为你有一些额外的问题,但它绝对不能用于前导斜杠。您可能还想阅读本文,这可能有所帮助:http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/ – GreyBeardedGeek

回答

13

为了将来的参考,经过多次调试后我发现了这个问题。原来的Eclipse正在建造我的“核心”库作为一个罐子,但随着Web应用程序封装布局,所以我的资源,而不是设在这里:

/company/config/spring-config.xml 

它位于:

/WEB-INF/company/config/spring-config.xml 
导致问题的

。我之前几次检查过这个罐子,但从未注意到隐藏在外面的鬼祟的“/ WEB-INF”。

删除项目并将其重新导入Eclipse(通过Maven pom.xml文件)不足以解决问题。

我不得不手动删除特定于Eclipse的.project,.classpath和.settings文件。当我这样做并重新导入项目时,一切都是固定的。

本课的道德是:总是检查您的资源路径,当例外说“找不到文件”。