java 加载 application-context.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4545968/
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
Loading application-context.xml
提问by theJava
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
My application-context.xml resides inside com.myname.mysubpackage.spring.application-context.xml
我的 application-context.xml 驻留在里面 com.myname.mysubpackage.spring.application-context.xml
How to get it loading...
怎么获取加载中...
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
I get this error.
我收到这个错误。
回答by Jigar Joshi
new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
Your file should be directly in your default package .
您的文件应该直接在您的默认包中。
if you are using maven then best place is to put it in resource
dir
如果你使用 maven 那么最好的地方是把它放在resource
dir
回答by Erhan Bagdemir
if you have problems with understanding of classpaths, alternatively, you can call your context file from file system like this (but not recommended, it solves your problem temporarily) :
如果您对类路径的理解有问题,或者,您可以像这样从文件系统调用您的上下文文件(但不推荐,它可以暂时解决您的问题):
File file = new File("/path/" + "Test.xml" );
FileSystemResource fileResource = new FileSystemResource( file );
XmlBeanFactory mFactory = new XmlBeanFactory( fileResource );
But the best usage is to put your config files in a resource directory and to add this directory into your classpath. with maven it is really easy to define.
但最好的用法是将您的配置文件放在资源目录中,并将此目录添加到您的类路径中。使用 maven 真的很容易定义。