Reputation: 33
There is some error while I am doing a sample project in spring..the error is like this...Test.java, applicationContext.java,Student.java this files are in src folder....
Oct 29, 2013 11:37:15 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e458c2: startup date [Tue Oct 29 11:37:15 IST 2013]; root of context hierarchy
Oct 29, 2013 11:37:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.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.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at springapplication.Main.main(Main.java:13)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 13 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second).......
My code is this...
Test.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class Test {
public static void main(String[] args){
Resource resource=new ClassPathResource("applicationConteXt.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Student student=(Student)factory.getBean("studentbean");
student.displayinfo();
}
}
Upvotes: 2
Views: 25899
Reputation: 1025
solution which worked for me
first point - where spring.xml file should be placed
can refer screenshot attached below
second thing : spring.xml file code
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="vehicle" class="org.example.Car"></bean>
</beans>
in place of defining bean definition in beans tag you may also use DOCTYPE, then spring.xml will look like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="vehicle" class="org.example.Car"></bean>
</beans>
lastly : how to call spring.xml file from main method
can refer below screenshot
hope it solved your problem, cheers :)
Upvotes: 0
Reputation: 101
beans.xml inside "com/mypackage" (Ex: com\mypackage\beans.xml);
How I was using
ClassPathXmlApplicationContext("beans.xml"); ----> ERROR.
There are three solutions:
ClassPathXmlApplicationContext( com/**default package name**/beans.xml);
ClassPathXmlApplicationContext("com/mypackage/beans.xml");
beans.xml
inside ProjectName\src
ClassPathXmlApplicationContext("beans.xml")
Upvotes: 1
Reputation: 3429
You gotta put it in resources
directory of maven project. It will work.
Upvotes: 4
Reputation: 21
With my Eclipse Java project, I fixed it by making the folder above the relative path a source folder.
Upvotes: 1
Reputation: 82
your applicationContext.xml is not in class path like not in src(source) folder of project. place your applicationContext.xml in src folder. this will work
Upvotes: 0