1
votes

I am just learning spring and my first program is to load a bean xml and instantiate the classes.

I will be using spring core and I have created a Java project. Created a package called:

com.lecture

and put the java files there as well as the bean file spring-beans.xml

<beans>
    <bean id="rkdf" class="com.college.RKDFCollege"/>
    <bean id="cseLect" class="com.college.CSELecturer"/>
</beans>

But when I am running the main class which has this code:

ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml");

it is giving me errors

Dec 07, 2014 9:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [com.lecture.spring-beans.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com.lecture.spring-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com.lecture.spring-beans.xml] cannot be opened because it does not exist

I am new to this I dont have any idea so far! In the project module settings, I have added JDK and Spring jars. Guidance is appreciated.

2
A file named com.lecture.spring-beans.xml is not there on the classpath. Do you have s file with that name in your filesystem?Gábor Bakos

2 Answers

1
votes

Try using the following path to the XML: "com/lecture/spring-beans.xml"

ClassPathXmlApplicationContext treats your class path as a file system.

0
votes

ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml"); is searching com.lecture.spring-beans.xml file in the class path which I believe is not the name of the file you have created. I believe you have created file spring-beans.xml

Make sure spring-beans.xml file is in class path. Else you can put the spring-beans.xml in resource folder as well. Then load the file as:

ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");