Java 使用 ClassPathXmlApplicationContext 函数时的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23489946/
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
Exception when using ClassPathXmlApplicationContext function
提问by Lalit_Bhanot
I was using the ClassPathXmlApplicationContextto read my xml. But on execution I am getting the error as stated under . I have also checked the file names and I have also executed the same program using XmlBeanFactoryfunction which returns the desired output
我正在使用ClassPathXmlApplicationContext来读取我的 xml。但是在执行时,我收到了如下所述的错误。我还检查了文件名,并且还使用XmlBeanFactory函数执行了相同的程序,该函数返回所需的输出
Here is my code
这是我的代码
package org.lalit.springsession;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class DrawingApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml")) ;
Triangle triangle= (Triangle) factory.getBean("triangle");
triangle.draw();
*/
BeanFactory factoryObj = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle= (Triangle) factoryObj.getBean("triangle");
triangle.draw();
}
}
}
These are Exceptions which I am facing.
这些是我面临的例外。
May 06, 2014 1:53:42 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14a1ee92: startup date [Tue May 06 13:53:42 IST 2014]; root of context hierarchy
May 06, 2014 1:53:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.lalit.springsession.DrawingApp.main(DrawingApp.java:24)
Caused by: java.io.FileNotFoundException: class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
采纳答案by M. Deinum
Your spring.xml
isn't on your classpath, it resides somewhere on your file system. Your XmlBeanFactory
is loading the file from the file system the ClassPathXmlApplicationContext
defaults to the classpath.
你spring.xml
不在你的类路径上,它驻留在你的文件系统上的某个地方。您XmlBeanFactory
正在从文件系统加载文件,ClassPathXmlApplicationContext
默认为类路径。
Either move the file to your classpath or modify the constructing of ClassPathXmlApplicationContext
and prefix with file:
.
将文件移动到您的类路径或修改ClassPathXmlApplicationContext
和 前缀的构造file:
。
BeanFactory factoryObj = new ClassPathXmlApplicationContext("file:spring.xml");
回答by Mohammed Altaf
Your configuration is correct.The problem here is the spring file is not loaded by the Drawing class So add your spring.xml file to the classpath by moving spring.xml to the src folder of your project..
你的配置是正确的。这里的问题是绘图类没有加载 spring 文件所以通过将 spring.xml 移动到项目的 src 文件夹,将你的 spring.xml 文件添加到类路径中。