XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
我的application-context.xml位于com.myname.mysubpackage.spring.application-context.xml
如何加载...
INFO - XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [application-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [application-context.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.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at com.mysticcoders.mysticpaste.services.CrudService.main(CrudService.java:9)
Caused by: java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 4 more
我收到此错误。
答案 0 :(得分:3)
new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
您的文件应该直接在您的默认包中。
如果你使用maven,那么最好的地方就是把它放在resource
dir
答案 1 :(得分:0)
如果你对类路径的理解有问题,或者你可以像这样从文件系统调用你的上下文文件(但不建议,它暂时解决你的问题):
File file = new File("/path/" + "Test.xml" );
FileSystemResource fileResource = new FileSystemResource( file );
XmlBeanFactory mFactory = new XmlBeanFactory( fileResource );
但最好的用法是将配置文件放在资源目录中,并将此目录添加到类路径中。使用maven,它很容易定义。