简体   繁体   中英

Eclipse not able to find Spring configuration file

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.

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");

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.

As you are using a full filepath for your Beans.xml example, use

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

BUT it is not recommended to do this. Use the ClassPathXmlApplicationContext for this instead.

Then move Beans.xml into the classpath. The simplest way to do this is to move it to the root of your java source if not using Maven or src/main/resources if using Maven

If it's not much of a bother then try using Spring Tool Suite . It's a Spring friendly IDE based on Eclipse, so that you don't have to depend on spring/maven plugin configurations. All you have to do is go and create a Spring Project instead of Java project and rest all the settings will be handled for you.

If you are using Spring Tool Suite (STS), it may be the case that when you create a Maven project the src/main/resources directory was configured with "Excluded: . ". In other words, STS sets your src/main/resources directory to have all its contents excluded from output by default.

How to fix it:

  1. Project properties (Alt+Enter) -> Java Build Path -> Source
  2. On src/main/resources, you may see "Excluded: . "
  3. Select this item and click on Edit...
  4. Remove the . exclusion pattern
  5. Click OK. Voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM