如何解决BeanDefinitionStoreException:从ServletContext资源[/WEB-INF/dispatcher-servlet.xml]解析XML文档的IOException?

时间:2014-09-15 17:45:58

标签: java spring spring-mvc web

错误堆栈跟踪:

SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]   

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:beans="http://springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context/spring-context.xsd       
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/task/spring-task.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.exam.www" />

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="Results" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />



    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp"
          p:suffix=".jsp" /> 

  <!--   <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>

    <bean class=
    "org.springframework.web.servlet.view.tiles2.TilesConfigurer"> -->
 <!--  <property name="definitions">
    <list>
      <value>/WEB-INF/views/views.xml</value>
    </list>
  </property> 
</bean> -->




</beans>

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!--
        CSRF protection. Here we only include the CsrfFilter instead of all of Spring Security.
        See http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf for more information on
        Spring Security's CSRF protection
    -->
<!--    <bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter">
        <constructor-arg>
            <bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/>
        </constructor-arg>
    </bean> -->
    <!--
        Provides automatic CSRF token inclusion when using Spring MVC Form tags or Thymeleaf. See
        http://localhost:8080/#forms and form.jsp for examples
    -->
    <!--  <bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor"/>

-->
</beans>

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

  <!-- <display-name>Spring With MongoDB Web Application</display-name> -->

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <welcome-file-list>
       <welcome-file>/search.jsp</welcome-file>
    </welcome-file-list> 
</web-app>

我在网上尝试了几种解决方案 1)赋予所有文件读,写权限 - 不起作用 2)添加init-param - 不起作用 3)明确地给出dispatcherservlet的路径为/WebContent/WEB-INF/dispatcher-servlet.xml - 不起作用。

我在Apache tomcat server v7.0上运行项目时出现此错误

过去4天我对此问题感到震惊。请帮我。

以下是有效的解决方案。

你的战争没有dispatcher-servlet.xml。 1)项目没有webapp文件夹。使用maven创建项目时要注意。您可以按照此处提到的步骤操作 http://blog.manishchhabra.com/2013/04/spring-data-mongodb-example-with-spring-mvc-3-2/ 你会使用一些奇怪的maven原型创建项目。试试上面的链接就行了。

我没有在互联网上找到其他解决方案。 :)

5 个答案:

答案 0 :(得分:5)

Spring为Web应用程序创建了两个应用程序上下文。第一个是包含应用程序bean的根应用程序上下文,例如DAO服务对象等。使用context-param contextConfigLocation配置此上下文(在您的情况下为applicationContext.xml)。第二个是包含您的web特定bean的子Web应用程序上下文。这是使用调度程序servlet的init-param contextConfiguration配置的。

在您的情况下,您已复制了两者的配置文件。按如下所示更改您的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

  <!-- <display-name>Spring With MongoDB Web Application</display-name> -->

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <welcome-file-list>
       <welcome-file>/search.jsp</welcome-file>
    </welcome-file-list> 
</web-app>

答案 1 :(得分:0)

尝试在web.xml

中更改<param-value>WEB-INF/dispatcher-servlet.xml</param-value>上的<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

答案 2 :(得分:0)

首先,dispatcher-servlet.xml是spring mvc的配置,它不适用于应用程序。所以你应该删除&lt; context-param&gt;您的web.xml中的标记。

然后spring mvc将根据servlet-name调度程序获取xml文件。所以你也可以删除标签&lt; init-param&gt;在web.xml中&lt; servlet&gt;。

尝试查看spring mvc是否可以找到xml auto

答案 3 :(得分:0)

确保在 beans 标记中声明的所有 xsd 在该位置都可用。

答案 4 :(得分:-1)

将mvc-config文件替换为uri,如下所示:

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value>
</context-param>