2012-10-05 69 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文件。當我這樣做並重新導入項目時,一切都是固定的。

本課的道德是:總是檢查您的資源路徑,當例外說「找不到文件」。