Eclipse not able to find Spring configuration file

21,794

Solution 1

Beans.xml should be in classpath. You cannot give full physical path of xml file for ClassPathXmlApplicationContext . Please check if Beans.xml is there in build path of eclipse.

Solution 2

Try this

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");

And of course your Beans.xml must be in classpath.

Update or maybe

ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");
Share:
21,794
user496934
Author by

user496934

Updated on October 12, 2020

Comments

  • user496934
    user496934 over 3 years

    I am wetting my hands in Spring and using Eclipse along with Spring. I have written a very simple Spring application with eclipse to inject a property in a bean. However, when I am running my application, Spring is throwing exception and it seems that the Spring is not able to find the Spring configuration file. Below is the stacktrace --

    INFO: Loading XML bean definitions from class path resource [Beans.xml]
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.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)
    

    I have tried the following -- Give the full path in the ClassPathXmlApplicationContext method like --

    ApplicationContext context = new ClassPathXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");
    

    I have also updated the ClassPath variable in windows to add the path for my spring configuration file. But nothing worked. Any idea would be highly appreciated.

  • StellaMaris
    StellaMaris about 8 years
    I also have the problem that I cant load the Bean.xml. Although I added the file to the classpath I only can loaded via "classpath*:Beans.xml". How can I loaded via "file:Beans.xml"?
  • Aleksandr M
    Aleksandr M about 8 years
    @StellaMaris If the classpath* is working why do you need to load via file:?
  • StellaMaris
    StellaMaris about 8 years
    Because I am interested in why only classpath* is working. And I have an other example where I try to load hibernate.cfg.xml and therefor I cant use classpath*.
  • StellaMaris
    StellaMaris about 8 years
    Yes, if I try to use new ClassPathXmlApplicationContext("file:spring/config/Bean.xml"‌​) it fails, although I added it to my Java Build Path > Sources.
  • Aleksandr M
    Aleksandr M about 8 years
    @StellaMaris You can try to use full path with file:.
  • StellaMaris
    StellaMaris about 8 years
    I already tried new ClassPathXmlApplicationContext("file:C:\\Users\\admin\\works‌​pace\\ssf\\samples\\‌​customcontroller\\sr‌​c\\resources\\spring‌​\\config\\BeanLocati‌​ons.xml"); and new ClassPathXmlApplicationContext("file:ssf/samples/customcontr‌​oller/src/resources/‌​spring/config/BeanLo‌​cations.xml"); but it doesn't work.