Eclipse not able to find Spring configuration file
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");
user496934
Updated on October 12, 2020Comments
-
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 about 8 yearsI 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 about 8 years@StellaMaris If the
classpath*
is working why do you need to load viafile:
? -
StellaMaris about 8 yearsBecause 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 useclasspath*
. -
StellaMaris about 8 yearsYes, 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 about 8 years@StellaMaris You can try to use full path with
file:
. -
StellaMaris about 8 yearsI already tried
new ClassPathXmlApplicationContext("file:C:\\Users\\admin\\workspace\\ssf\\samples\\customcontroller\\src\\resources\\spring\\config\\BeanLocations.xml");
andnew ClassPathXmlApplicationContext("file:ssf/samples/customcontroller/src/resources/spring/config/BeanLocations.xml");
but it doesn't work.