java - 解析 XML ServletContext 资源 BeanDefinitionStoreException

标签 java xml spring

我知道类似的问题问了很多次,但我不明白出了什么问题。帮我解决这个问题。我尝试使用 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>

项目结构:

enter image description here

最佳答案

在 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/

相关文章:

spring - @PostConstruct 方法在 Spring 中不被调用

java - 使用 google-rfc-2445 createDateTimeIterator 生成日期范围时排除结束日期(UNTIL)

java - Mule 3.2 中的分离器

java - 读取驻留在从另一个 jar 文件调用的 jar 中的文件

java - 对 XML 配置文件使用 @ImportResource 的解决方法

java - 类路径资源 [applicationContext.xml] 无法打开,因为它不存在使用 java 配置

java - Maven - 如何配置 Maven 以满足这些要求

java - 部署vaadin应用程序时出错

java - 如何在java中将多个对象分配给分割字符串的一部分?

java - 将ScrollView插入布局