Java IOException 从类路径资源解析 XML 文档 - HTTP 状态 500 - Servlet.init()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18404480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOException parsing XML document from class path resource - HTTP Status 500 - Servlet.init()
提问by MattTheHack
I am building a simple Spring MVC Maven built java WAR application. My Maven project cleans and builds successfully and deploys to the server with no errors using the eclipse tomcat plugin. However, when I try to run the application Servlet, I get the following exception :
我正在构建一个简单的 Spring MVC Maven 构建的 java WAR 应用程序。我的 Maven 项目使用 eclipse tomcat 插件成功清理和构建并部署到服务器,没有错误。但是,当我尝试运行应用程序 Servlet 时,出现以下异常:
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
My project configuration is as follows :
我的项目配置如下:
web.xml
网页.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
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>
And my Java class looks as follows :
我的 Java 类如下所示:
public class FileWatcher {
....
}
I am new to using this system so I'm not sure where I have gone wrong, can anyone help me solve this error?
我是使用这个系统的新手,所以我不确定我哪里出错了,有人能帮我解决这个错误吗?
回答by Prabhakaran Ramaswamy
Create the spring named folder under WEB-INF.
在 WEB-INF 下创建名为 spring 的文件夹。
Create the file-watcher.xml file inside the spring folder.
在 spring 文件夹中创建 file-watcher.xml 文件。
After that your path will be
之后你的路径将是
yourproject\src\main\webapp\WEB-INF\spring\file-watcher.xml
yourproject\src\main\webapp\WEB-INF\spring\file-watcher.xml
Paste the below XML content to file-watcher.xml
将以下 XML 内容粘贴到 file-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>
Then run your server. It will work.
然后运行你的服务器。它会起作用。
回答by Samson 'Melah' Nyabanga
One suggestion i might give you is for you check the location of your file-watcher.xml
我可能会给你的一个建议是检查你的file-watcher.xml的位置
If you are using Maven you might want to consider putting your file-watcher.xmlinside the resourcesfolder.
如果您使用 Maven,您可能需要考虑将file-watcher.xml放在资源文件夹中。
And in your web.xmlfile kindly declare your tag as shown below:
并在您的web.xml文件中请声明您的标签如下图:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:file-watcher.xml</param-value>
</context-param>
NB: Make sure that src/main/java/resourcesis on your classpath and bear in mind that in Maven, the standard directory for resources is src/main/resources, so I suggest you put this file there.
注意:确保src/main/java/resources在你的类路径上,并记住在Maven 中,资源的标准目录是src/main/resources,所以我建议你把这个文件放在那里。