2017-08-11 84 views
0

我知道类似的问题问很多时间,但我不明白什么是错的。帮我解决这个问题。我尝试运行我的应用程序用SpringMVC但我得到:解析XML ServletContext资源BeanDefinitionStoreException

11月 - 2017年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> 

项目结构:

enter image description here

回答

1

在您的网络xml开始宣布ContextLoaderListener。 正如你可以在这个文件 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/ContextLoaderListener.html#ContextLoaderListener-- 中所看到的,当您使用默认的构造函数(不带参数),它充当跟随

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

默认contextConfigLocation确切地说是/WEB-INF/applicationContext.xml。在你的项目中,该文件不存在,但是类ContextLoaderListener需要该文件,如果没有找到它,请抛出你提到的异常。 您可以通过将您的配置设置为参数来解决您的问题。

+0

你是什么意思_pasing为param_? – Pavel

+0

使用标签param-name和param-value将参数传递给ContextConfig的构造函数Location –