簡體   English   中英

Spring內部的war無法在內部jar文件中找到classpath資源

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM