theJava
theJava

Reputation: 15034

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 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.

Upvotes: 2

Views: 10162

Answers (2)

Erhan Bagdemir
Erhan Bagdemir

Reputation: 5327

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.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

 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

Upvotes: 3

Related Questions