【问题标题】:Spring cannot find file in classpath, but it's right there at the root of the war fileSpring在类路径中找不到文件,但它就在war文件的根目录下
【发布时间】:2019-02-02 14:35:23
【问题描述】:

我有一个使用标准 Maven Web 应用程序布局的项目,部署在 Eclipse Photon 的 Tomcat 8.5 中(带有最新更新)。

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 而不是 other-spring-stuff.xml 的任何原因?它只是一个例子还是你重命名了文件?
  • 我只是想让这个问题更加不言自明。实际文件是 spring.xml
  • 你的 other-spring-stuff.xml 还在 src/main/resources 下吗?你能试试不带/classpath*:other-spring-stuff.xml
  • 我决定删除项目并从主分支重新结帐。现在我的错误变成了web.xml is missing and <failOnMissingWebXml> is set to true,就像这个未回答的问题一样:stackoverflow.com/questions/51385888/…

标签: spring eclipse maven classloader


【解决方案1】:

原来的问题(spring.xml not found)和次要的问题(web.xml丢失的虚假报告)都通过以下步骤解决: p>

  1. 从工作区中删除项目,但将其保留在文件系统中
  2. 转到项目的文件系统位置并删除.project.classpath.settings/
  3. 将项目重新导入Eclipse,选择Existing Maven Project作为项目类型
  4. 为防止再次发生:从 git 中删除 .project.classpath.settings/*,然后将它们添加到 .gitignore

我认为根本原因是检入到 repo 的这些 Eclipse 特定配置文件之一已损坏和/或与我的 Eclipse 版本不兼容。另外:这些文件似乎包含对工作区中事物的绝对路径引用,因此当项目在未修改的情况下签出到其他位置时它们会中断

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 2012-11-01
    • 2019-04-30
    • 2015-08-24
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多