0
votes

I have a very simple spring test app. But I get exception even though everything seems to be on order. I might be missing something. Please check the pic to see the project structure and web.xml file contains as well as exception:-

efinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/servlet-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/servlet-context.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)

enter image description here

1
WEB-INF is not a classpath resource but a servlet resource... remove classpath: prefix or place your XML files inside src/main/resources from where they are copied to WEB-INF/classes (which is on classpath).Pavel Horal

1 Answers

3
votes

There are two kinds of resources in a servlet environment:

  • servlet resources - files uner the root of the web application (loaded via ServletContext)
  • classpath resources - resources on web application's classpath (loaded via ClassLoader)

When Spring is supposed to load its configuration it needs to know which mechanism to use.

  • classpath:foo/bar.xml - will load as classpath resource
    • checking WEB-INF/classes, contents of WEB-INF/lib/*.jar and other shared servlet container's classpath locations
    • when using maven and its project structure, all files from src/main/resources will be placed on classpath
  • foo/bar.xml - will load as servlet resource
    • when using maven and its project structure the src/main/webapp folder is the root of your application

TL;DR As I wrote in the comment, either remove classpath: prefix when referencing XML file or move your XML file to src/main/resources and remove the WEB-INF part.