Lalit_Bhanot Lalit_Bhanot - 1 year ago 38
Java Question

Exception when using ClassPathXmlApplicationContext function

I was using the ClassPathXmlApplicationContext to read my xml. But on execution I am getting the error as stated under . I have also checked the file names and I have also executed the same program using XmlBeanFactory function which returns the desired output

Here is my code

package org.lalit.springsession;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

public class DrawingApp {

public static void main(String[] args) {
// TODO Auto-generated method stub



/*
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml")) ;
Triangle triangle= (Triangle) factory.getBean("triangle");
triangle.draw();
*/

BeanFactory factoryObj = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle= (Triangle) factoryObj.getBean("triangle");
triangle.draw();
}


}

These are Exceptions which I am facing.

May 06, 2014 1:53:42 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org[email protected]14a1ee92: startup date [Tue May 06 13:53:42 IST 2014]; root of context hierarchy
May 06, 2014 1:53:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
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:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.lalit.springsession.DrawingApp.main(DrawingApp.java:24)
Caused by: java.io.FileNotFoundException: class path resource [C:/Users/Lalit/workspace/DemoSpring/spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more

Answer Source

Your spring.xml isn't on your classpath, it resides somewhere on your file system. Your XmlBeanFactory is loading the file from the file system the ClassPathXmlApplicationContext defaults to the classpath.

Either move the file to your classpath or modify the constructing of ClassPathXmlApplicationContext and prefix with file:.

    BeanFactory factoryObj = new ClassPathXmlApplicationContext("file:spring.xml");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download