2013-08-23 44 views
1

我正在構建一個簡單的Spring MVC Maven構建的java WAR應用程序。我的Maven項目使用eclipse tomcat插件成功清理並構建,並且無錯地部署到服務器。然而,當我嘗試運行應用程序的Servlet,我得到以下異常:IOException從類路徑資源解析XML文檔 - HTTP狀態500 - Servlet.init()

Aug 23, 2013 2:35:29 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
INFO: FrameworkServlet 'FileWatcher': initialization started 
Aug 23, 2013 2:35:29 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing WebApplicationContext for namespace 'FileWatcher-servlet': startup date [Fri Aug 23 14:35:29 BST 2013]; root of context hierarchy 
Aug 23, 2013 2:35:29 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [spring/file-watcher.xml] 
Aug 23, 2013 2:35:29 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/file-watcher.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/file-watcher.xml] cannot be opened because it does not exist 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:160) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) 
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 
Caused by: java.io.FileNotFoundException: class path resource [spring/file-watcher.xml] cannot be opened because it does not exist 
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) 
    ... 33 more 

我的項目配置如下:

的web.xml

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


    <display-name>FileWatcher</display-name> 

    <description>File Watcher</description> 


    <!-- To load multiple context files here just put a space between your files 
     listed, like here http://blog.codehangover.com/load-multiple-contexts-into-spring/ --> 
    <servlet> 
     <servlet-name>FileWatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:spring/file-watcher.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 



    <servlet-mapping> 
     <servlet-name>FileWatcher</servlet-name> 
     <url-pattern>/FileWatcher/*</url-pattern> 
    </servlet-mapping> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/file-watcher.xml</param-value> 
    </context-param> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 

</web-app> 

POM。 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" 
    default-autowire="byName"> 

    <bean id="FileWatcher" class="com.test.FileWatcher" init-method="initialize" /> 

</beans> 

我的Java類,如下所示:

public class FileWatcher { 
    .... 
    } 

我是新來使用這個系統,所以我不知道,我出了問題,誰能幫我解決這個問題?

+1

你在'WEB-INF/classes'中有'spring/file-watcher.xml'嗎? – sanbhat

+0

嗨,我沒有WEB-INF/classes文件夾,我應該創建一個嗎? 編輯: 從頭開始,我檢查了目標目錄,並在那裏找到一個包含文件watcher.xml文件的WEB-INF/classes目錄。它需要在一個春天目錄嗎? – MattTheHack

+1

你的路徑應該與web中的上下文參數相匹配。xml文件​​contextConfigLocation classpath:spring/file-watcher.xml Prabhakaran

回答

2

在WEB-INF下創建彈簧命名文件夾。

在spring文件夾中創建file-watcher.xml文件。

之後,你的路徑將是

yourproject的\ src \主\ web應用\ WEB-INF \彈簧\文件watcher.xml

粘貼下面的XML內容的文件watcher.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" 
     default-autowire="byName"> 

    <bean id="FileWatcher" class="com.test.FileWatcher" init-method="initialize" /> 

    </beans> 

然後運行您的服務器。它會工作。

+0

您好, 感謝您的幫助。我仍然得到錯誤代碼500.任何想法爲什麼春天文件夾不會自動生成?每次安裝完成後,我都要繼續將它添加到目標/ WEB-INF目錄中 – MattTheHack

+0

請放置在這個用戶管理\ src \ main \ webapp \ WEB-INF \ spring \ file-watcher.xml中。用戶管理是我的項目根路徑。如果你運行mvn clean,不要在目標文件夾中放置任何資源。 – Prabhakaran

+0

這工作正常,但仍然是一個錯誤代碼500. 該文件夾結束在這裏: ... \ FileSystem \目標\ watchFileSystem-0.0.1-SNAPSHOT \ WEB-INF \春天 – MattTheHack

0

一個建議,我可能給你的是你檢查你的文件watcher.xml的位置

如果您正在使用Maven,你可能要考慮把你的文件watcher.xml資源文件夾。

而在你的web.xml文件敬請聲明你標籤,如下圖所示:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:file-watcher.xml</param-value> 
    </context-param> 

注意:確保的src/main/JAVA /資源是在classpath和熊記住,在Maven,資源的標準目錄是src/main/resources,所以我建議你把這個文件放在那裏。

相關問題