4
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;

public class Test {
    public static void main(String args[]){

    Resource res= new ClassPathResource("E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(res);

    Employee s=(Employee)factory.getBean("e");

    }
}

Above is my program and the error show is:

Oct 13, 2015 8:42:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/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: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 contructor.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.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)

What could be the possible problem? please help as i am new to springs

I am facing same problem, how can i solve it?

Prasad
  • 125
  • 1
  • 1
  • 7
abhishek shivdekar
  • 69
  • 1
  • 2
  • 11

2 Answers2

4

You are trying to get a reference to a file as a classpath resource but the path you have given is not a path of a file inside the classpath.

Instead of harcoding the full path to the file, use a path that is relative to the root source of the project. If src is the root source directory, use this:

Resource res = new ClassPathResource("/contructor/applicationContext.xml");
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

Because it is ClassPathResource I think contructor/applicationContext.xml is enough

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
Saravana
  • 12,647
  • 2
  • 39
  • 57