Java 加载application-context.xml
My application-context.xml位于Java 加载application-context.xml,java,spring,Java,Spring,My application-context.xml位于com.myname.mysubpackage.spring.application-context.xml 如何让它加载 XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource( "application-context.xml")); INFO-XmlBeanDefinitionReader-从类路径资源[applica
com.myname.mysubpackage.spring.application-context.xml
如何让它加载
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
INFO-XmlBeanDefinitionReader-从类路径资源[application context.XML]加载xmlbean定义
线程“main”org.springframework.beans.factory.BeanDefinitionStoreException中出现异常:IOException从类路径资源[application context.XML]解析XML文档;嵌套异常为java.io.FileNotFoundException:无法打开类路径资源[application context.xml],因为它不存在
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:73)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:61)
位于com.mysticcoders.mysticpaste.services.CrudService.main(CrudService.java:9)
原因:java.io.FileNotFoundException:无法打开类路径资源[application context.xml],因为它不存在
位于org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 4更多
我得到这个错误
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
您的文件应该直接位于默认包中
如果您使用的是maven,那么最好的地方就是将其放入
资源目录如果您在理解类路径方面有问题,或者,您可以像这样从文件系统调用上下文文件(但不推荐,它暂时解决了您的问题):
但最好的用法是将配置文件放在资源目录中,并将该目录添加到类路径中。有了maven,定义起来真的很容易 把它移到主资源目录下运行得很好。谢谢
new XmlBeanFactory(new ClassPathResource(
"application-context.xml"));
File file = new File("/path/" + "Test.xml" );
FileSystemResource fileResource = new FileSystemResource( file );
XmlBeanFactory mFactory = new XmlBeanFactory( fileResource );