【问题标题】:Can't find applicationContext.xml from jar in classpath无法从类路径中的 jar 中找到 applicationContext.xml
【发布时间】:2025-12-11 01:50:01
【问题描述】:

我有 Spring 的 Maven 项目。我在类路径中有后端项目作为 jar(添加为 maven 依赖项)。后端项目再次使用 Spring 并拥有自己的 applicationContext.xml。在前端项目中,我有 applicationContext.xml,我想从后端访问应用程序上下文,如下所示:

<import resource="classpath:applicationContext.xml" />

但我得到了:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:applicationContext.xml]
Offending resource: ServletContext resource [/WEB-INF/appContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

也试过了:

<import resource="classpath*:applicationContext.xml" />

<import resource="classpath:/applicationContext.xml" />

【问题讨论】:

  • 在 src/main/resources 中有 applicationContext.xml 吗?
  • 是的。在后端项目(jar)中它就在那里。在 FE(我想使用 jar 中的那个)中,它位于 WEB-INF

标签: java xml spring configuration


【解决方案1】:

如果我理解正确,您希望前端访问后端项目的 applicationContext.xml。那么 classpath* 是正确的说法,你应该确保两个配置文件的命名空间是相同的。

例如,这种情况下不起作用

前端:

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

后端:

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

【讨论】:

    最近更新 更多