我知道类似的问题问了很多次,但我不明白出了什么问题。帮我解决这个问题。我尝试使用 SpringMVC 运行我的应用程序,但我得到:
11-Aug-2017 13:06:29.471 SEVERE [RMI TCP Connection(2)-127.0.0.1]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource
我在web.xml
classpath:spring-context.xml
中设置文件路径:
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Process application servlet -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
项目结构:
最佳答案
在 Web xml 的开头声明了 ContextLoaderListener
。
正如您在本文档中看到的
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ContextLoaderListener.html#ContextLoaderListener--
当您使用默认构造函数(不带参数)时,它的行为如下
Create a new ContextLoaderListener that will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params.
默认的contextConfigLocation
正是/WEB-INF/applicationContext.xml
。在您的项目中,该文件不存在,但 ContextLoaderListener 类需要该文件来查找该文件,如果找不到它,则抛出您提到的异常。
您可以通过将配置作为参数传递来解决您的问题。
关于java - 解析 XML ServletContext 资源 BeanDefinitionStoreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632938/