严重:上下文初始化失败org.springframework.beans.factory.BeanDefinitionStoreException:

时间:2017-04-05 11:16:01

标签: spring-mvc

我是Spring MVC的新手。当我尝试在tomcat中运行我的动态Web项目时,我收到以下错误8.5这是我在Spring的第一个项目

错误

  

SEVERE:将上下文初始化事件发送给侦听器的异常   类的实例   org.springframework.web.context.ContextLoaderListener           org.springframework.beans.factory.BeanDefinitionStoreException:   IOException从ServletContext资源解析XML文档   [/WEB-INF/applicationContext.xml的];嵌套异常是   java.io.FileNotFoundException:无法打开ServletContext资源   [/WEB-INF/applicationContext.xml的]             在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)             在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)             在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)             在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)             在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)             在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)             在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)             at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)             在org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)             在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)             在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)             在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)             在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)             在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)             at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)             在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)             在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1419)             在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1409)             at java.util.concurrent.FutureTask.run(FutureTask.java:266)             在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)             at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)             在java.lang.Thread.run(Thread.java:745)           引起:java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/applicationContext.xml]             在org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)             在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)             ......还有21个

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>SpringMVCTest</display-name>
      <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

      </welcome-file-list>

      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>

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

    </web-app>

我的dispatcher-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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

        </bean>

        <bean name="/" class="com.learn.spring.controller.BaseController"></bean>

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

    </beans>

我只是想在页面中打印消息,我是否需要applicationContext.xml文件?如果是,我如何在项目中添加它?我读到spring搜索[servlet-name] -servlet.xml文件。

1 个答案:

答案 0 :(得分:1)

您必须在 web.xml 文件中初始化 dispatcher-servlet.xml 文件。 更改您的web.xml文件,添加这些行。

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

这是您最后的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVCTest</display-name>
  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

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

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

</web-app>

您应该在 WEB-INF 文件夹下创建dispatcher-servlet.xml文件