开发者

Loading application-context.xml

XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
                "application-context.xml"));

My application-context.xml resides inside 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 c开发者_Python百科om.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.


 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜