10

私はこのように組織されたプロジェクトを持っています:

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にある場合、すべてが正常に機能します。

スタックトレースのエラーの一部から先頭が削除されていることに気づきました/。これはそれと関係があるのではないかと思います。

また、私は(残念ながら)Spring2.5を使用しています。

4

1 に答える 1

13

将来の参考のために、私は多くのデバッグの後に問題を理解しました。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ファイルを手動で削除する必要がありました。私がそれをしてプロジェクトを再インポートしたとき、すべてが修正されました。

レッスンの教訓は次のとおりです。例外に「ファイルが見つかりません」と表示されている場合は、常にリソースパスを確認してください

于 2012-10-07T00:42:52.510 に答える