Spring 在类路径中找不到文件,但它就在 war 文件的根目录下
Spring cannot find file in classpath, but it's right there at the root of the war file
我有一个使用标准 Maven 网络应用程序布局的项目,部署在 Tomcat 8.5 中的 Eclipse Photon(具有最新更新)中。
src\main\webapp\WEB-INF\applicationContext.xml
里面有一个<import resource="classpath:/other-spring-stuff.xml" />
我验证了通常位于src/main/resources
中的other-spring-stuff.xml
,出现在mvn install
生成的WAR
的根目录中。
但是在 Eclipse 中尝试 运行 Tomcat 中的应用程序时,我得到:
20:37:17 SEVERE: Servlet [servletSpringDispatcher] in web application [/myapp] threw load() exception - 2018-08-27 @{org.apache.catalina.core.StandardContext loadOnStartup}
java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:227)
我真的不知道这是 Maven、Eclipse 还是 Spring 的问题。
这是什么原因造成的?这实际上是在昨天之前运行良好的项目中,项目配置没有任何变化。
原问题(spring.xml未找到)和次要问题(漏报web.xml ) 都通过以下步骤得到解决:
- 从工作区中删除项目,但保留在文件系统中
- 转到项目的文件系统位置并删除
.project
、.classpath
和 .settings/
- 将项目重新导入Eclipse,select Existing Maven Project as project type
- 为了防止再次发生:从 git 中删除
.project
、.classpath
和 .settings/*
,然后将它们添加到 .gitignore
我认为根本原因是签入存储库的这些特定于 Eclipse 的配置文件之一已损坏 and/or 与我的 Eclipse 版本不兼容。另外:这些文件似乎包含对工作区中事物的绝对路径引用,因此当项目未经修改检出到不同位置时它们会中断。
我有一个使用标准 Maven 网络应用程序布局的项目,部署在 Tomcat 8.5 中的 Eclipse Photon(具有最新更新)中。
src\main\webapp\WEB-INF\applicationContext.xml
里面有一个<import resource="classpath:/other-spring-stuff.xml" />
我验证了通常位于src/main/resources
中的other-spring-stuff.xml
,出现在mvn install
生成的WAR
的根目录中。
但是在 Eclipse 中尝试 运行 Tomcat 中的应用程序时,我得到:
20:37:17 SEVERE: Servlet [servletSpringDispatcher] in web application [/myapp] threw load() exception - 2018-08-27 @{org.apache.catalina.core.StandardContext loadOnStartup}
java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:227)
我真的不知道这是 Maven、Eclipse 还是 Spring 的问题。
这是什么原因造成的?这实际上是在昨天之前运行良好的项目中,项目配置没有任何变化。
原问题(spring.xml未找到)和次要问题(漏报web.xml ) 都通过以下步骤得到解决:
- 从工作区中删除项目,但保留在文件系统中
- 转到项目的文件系统位置并删除
.project
、.classpath
和.settings/
- 将项目重新导入Eclipse,select Existing Maven Project as project type
- 为了防止再次发生:从 git 中删除
.project
、.classpath
和.settings/*
,然后将它们添加到.gitignore
我认为根本原因是签入存储库的这些特定于 Eclipse 的配置文件之一已损坏 and/or 与我的 Eclipse 版本不兼容。另外:这些文件似乎包含对工作区中事物的绝对路径引用,因此当项目未经修改检出到不同位置时它们会中断。