我使用 ClassPathXmlApplicationContext 来读取我的xml。但在执行时,我收到了错误,如下所述。我还检查了文件名,并且还使用 XmlBeanFactory 函数执行了相同的程序,该函数返回所需的输出
这是我的代码
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();
}
}
这些是我所面临的例外情况。
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
答案 0 :(得分:0)
您的spring.xml
不在您的类路径上,它位于文件系统的某个位置。您的XmlBeanFactory
正在从文件系统加载文件ClassPathXmlApplicationContext
默认为类路径。
将文件移至类路径或修改ClassPathXmlApplicationContext
的构造,并使用file:
作为前缀。
BeanFactory factoryObj = new ClassPathXmlApplicationContext("file:spring.xml");
答案 1 :(得分:0)
您的配置是正确的。这里的问题是Drawing类没有加载spring文件所以通过将spring.xml移动到项目的src文件夹中将spring.xml文件添加到类路径中。