2017-08-11 86 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 –