Spring cannot find file in classpath, but it's right there at the root of the war file

浪尽此生 提交于 2019-12-24 19:14:35

问题


I have a project using the standard Maven web app layout, deployed within Tomcat 8.5 in Eclipse Photon (with latest updates).

Inside of src\main\webapp\WEB-INF\applicationContext.xml, there is an <import resource="classpath:/other-spring-stuff.xml" />

I verified the other-spring-stuff.xml which is normally sitting in src/main/resources, appears in the root of the WAR generated by mvn install.

But when trying to run the app in Tomcat within Eclipse, I get:

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)

I don't really know whether this is a problem with Maven, Eclipse, or Spring.

What can cause this? This is actually in a project that was working fine until yesterday, and nothing changed on the project configuration.


回答1:


The original problem (spring.xml not found) and secondary problem (false report of missing web.xml) were both resolved by taking the following steps:

  1. Delete the project from the workspace, but keep it on the file system
  2. Go to the project's filesystem location and delete .project, .classpath, and .settings/
  3. Re-Import the project into Eclipse, select Existing Maven Project as project type
  4. To prevent reocurrence: remove .project, .classpath, and .settings/* from git and then add them to .gitignore

I believe the root cause is that one of these Eclipse-specific config files that was checked into the repo had become corrupted and/or incompatible with my version of Eclipse. Also: these files seem to contain absolute-path references to things in the workspace, so they break when the project is checked out unmodified to a different location.



来源:https://stackoverflow.com/questions/52049748/spring-cannot-find-file-in-classpath-but-its-right-there-at-the-root-of-the-wa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!