【问题标题】:Parsing of XML ServletContext resource BeanDefinitionStoreException解析 XML ServletContext 资源 BeanDefinitionStoreException
【发布时间】:2017-08-11 10:26:59
【问题描述】:

我知道问了很多次类似的问题,但我不明白哪里错了。帮我解决这个问题。我尝试使用 SpringMVC 运行我的应用程序,但我得到:

2017 年 8 月 11 日 13:06:29.471 严重 [RMI TCP 连接 (2)-127.0.0.1]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException 从 ServletContext 资源解析 XML 文档 [/WEB-INF/applicationContext.xml];嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源

我在web.xmlclasspath: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>

项目结构:

【问题讨论】:

    标签: java xml spring


    【解决方案1】:

    在 web xml 的开头,您声明了 ContextLoaderListener。 正如您在本文档中看到的 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ContextLoaderListener.html#ContextLoaderListener-- 当您使用默认构造函数(不带参数)时,它的行为如下

    创建一个新的 ContextLoaderListener,它将创建一个 Web 应用程序 基于“contextClass”和“contextConfigLocation”的上下文 servlet 上下文参数。

    默认的contextConfigLocation 正好是/WEB-INF/applicationContext.xml。在您的项目中,该文件不存在,但类 ContextLoaderListener 需要它来查找它,当找不到它时,抛出您提到的异常。 您可以通过将配置作为参数传递来解决您的问题。

    【讨论】:

    • 你是什么意思pasing as param
    • 使用标签param-name和param-value将参数传递给ContextConfig Location的构造函数
    猜你喜欢
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2013-10-21
    • 2013-09-23
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多