Spring cannot find bean xml configuration file if it exists

I am trying to create my first bean in Spring, but there was a problem loading the context. I have an XML bean configuration file in src / main / resources.

I get the following IOException:

An exception in the stream "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing an XML document from a class path resource [src / main / resources / beans.xml]; nested exception

java.io.FileNotFoundException: class path resource [src / main / resources / beans.xml] cannot be open because it does not exist

but I don’t understand this, since I am doing the following code test:

File f = new File("src/main/resources/beans.xml"); System.out.println("Exist test: " + f.exists()); 

which gives me the truth! resources is on the way to classes. What's wrong?

+65
spring javabeans configuration applicationcontext
Oct 15 '12 at 10:32
source share
17 answers

Thanks, but that was not the solution. I understood why it does not work for me.

Since I made an announcement:

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

I thought that I would refer to the project root directory when there was a beans.xml file. Then I put the configuration file in src / main / resources and changed the initialization to:

 ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/beans.xml"); 

it was still an IO exception.

Then the file remained in src / main / resources /, but I changed the declaration to:

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

and he solved the problem - maybe it will be useful for someone.

Thanks and welcome!

Edit:

Since I get a lot of people to solve the problem, and I had my first experience working with a Spring student a few years ago, I feel like I want to briefly explain why it works.

When the project is compiled and packaged, all files and sub-folders from 'src / main / java' in the project go to the root directory of the packaged jar (the artifact we want to create). The same rule applies to 'src / main / resources'.

This convention is followed by many tools, such as maven or sbt, when creating a project (note: as the default configuration!). When the code (from the message) was in working mode, it could not find anything like "src / main / resources / beans.xml" due to the fact that beans.xml was in the root bank (copied to /beans.xml in the created can / ear / war).

When using ClassPa, thanksmlApplicationContext the correct location declaration for xml beans definitions in this case was "/beans.xml", since this is the path where it belongs in the jar and then the classpath.

This can be checked by unpacking the jar with the archiver (i.e. rar) and see its contents in the directory structure.

I would recommend reading classpath articles as optional.

+133
Oct 18 '12 at 14:48
source share

Try the following:

new ClassPathXmlApplicationContext(" file: src/main/resources/beans.xml");

file: specify a value for file system resources, not the class path.

file path can be relative or system (/ home / user / Work / src ...)

+46
Dec 12 '13 at 15:10
source share

I also had a similar problem, but because of a slightly different reason, so share here if it can help anyone.

My file location

beans.xml file

How i used

ClassPathXmlApplicationContext("beans.xml");

There are two solutions.

  • Extract beans.xml from the package and put in the default package.
  • Specify a package name when using it.

ClassPathXmlApplicationContext("com/mypackage/beans.xml");

+18
Mar 13 '15 at 0:16
source share

src/main/resources is the source directory, you should not reference it directly. When you create / package the project, the content will be copied to the right place for your class path. Then you should download it as follows

 new ClassPathXmlApplicationContext("beans.xml") 

Or like that

 new GenericXmlApplicationContext("classpath:beans.xml"); 
+6
Feb 21 '17 at 23:09
source share

use it ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");

+4
Dec 26 '15 at 6:18
source share

I suspect that you are creating .war / .jar and, therefore, this is no longer a file, but a resource inside this package. Instead, try ClassLoader.getResourceAsStream (String path) .

+3
Oct. 15 '12 at 10:35
source share

You have looked at the src directory. The xml file does exist. But look at the bin / build class or directory where all your output classes are installed. I suspect that you only need the / beans.xml resource path to use.

+3
Oct. 15 '12 at 14:16
source share

In Spring, all source files are inside src / main / java. Similarly, resources are usually stored inside src / main / resources. Therefore, save the Spring configuration file inside the resource folder.

Make sure you have a ClassPath entry for your files inside src / main / resources.

In .classpath check the following 2 lines. If they are missing, add them.

 <classpathentry path="src/main/java" kind="src"/> <classpathentry path="src/main/resources" kind="src" /> 

So, if you have everything in place, the code below should work below.

ApplicationContext ctx = new ClassPas thanksmlApplicationContext ("Spring -Module.xml");

+2
Mar 01 '17 at 20:39
source share

This is because applicationContect.xml or any_filename.XML is not placed in the correct path.

Troubleshooting steps

1: Add the XML file to the resource folder.

2: if you do not have a resource folder. Create it by clicking on the new one, right-clicking on the new> Source Folder project, name it as a resource and place an XML file under it.

+2
Sep 29 '18 at 4:07
source share

Note that the first applicationContext is loaded as part of web.xml ; which is mentioned below.

 <context-param> <param-name>contextConfigLocation</param-name> <param-value>META-INF/spring/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>myOwn-controller</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>META-INF/spring/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

Where, as indicated below, the code will also try to create another applicationContext.

 private static final ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

See section the difference between beans.xml and applicationContext.xml

And if appliationContext.xml in <META-INF/spring/> declared with <import resource="beans.xml"/> , then this appliationContext.xml loads beans.xml under the same META-INF/spring location from appliationContext.xml .

Where how; in code; if listed below

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

It looks like beans.xml in WEB-INF/classes OR in eclipse src/main/resources .

[If you added beans.xml to src/main/resources , then it can be placed in WEB-INF/classes when creating a WAR.]

In this way, TWO strong> files are viewed completely .

I solved this problem by adding a search for the classpath when importing into applicationContext.xml as shown below

 <import resource="classpath*:beans.xml" /> 

and deleted the line ClassPathXmlApplicationContext("beans.xml") in the Java code, so that only one ApplicationContext will be loaded.

+1
Nov 16 '16 at 9:00
source share

I made the opposite majority. I use the Force IDE Luna Java EE and I put my Beans.xml file in the package; however, I was preceded by a Beans.xml line - for the ClassPa argument, thanks MLApplicationContext - with a relative path. So in my main application - the one that accesses the Beans.xml file - I have:

  ApplicationContext context = new ClassPathXmlApplicationContext("com/tutorialspoin/Beans.xml"); 

I also noticed that as soon as I moved the Beans.xml file to the package from the src folder, a Bean image appeared in the lower left part of the XML file icon, which was not there when the xml file was outside the package. This is a good indicator to let me know that the beans xml file is now accessible through ClassPa, thanks MLAppllicationsContext.

0
Jun 18 '15 at 14:17
source share

This is what worked for me:

  new ClassPathXmlApplicationContext("classpath:beans.xml"); 
0
Aug 16 '16 at 13:02
source share

If this problem still bothers you and you are developing using Eclipse, take a look at this Eclipse error: resource files from "src / main / resources" are not included in the classpath correctly

The solution seems to be to look at the project properties, Java build paths, source folders. Remove /src/main/resources and add it again. This makes Eclipse remind you to copy these files to the classpath.

This bug affected me when I used the "Neon" release of Eclipse. (And it was very difficult until I realized the simple fix just described)

0
Jan 08 '18 at 13:55
source share

Well, none of these answers work for me .. so I accidentally put my SpringProjectConfig.xml file into several positions, it doesn't work. [! [Enter image

description here] 1 ] 1

here is my mistake

 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [SpringProjectConfig]; nested exception is java.io.FileNotFoundException: class path resource [SpringProjectConfig] cannot be opened because it does not exist 
0
Dec 12 '18 at 1:08
source share

I experienced this problem and it drove me crazy; I eventually discovered the following in my POM.xml file that caused the problem:

 <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> </includes> </resource> </resources> 
0
Feb 19 '19 at 9:21
source share

I was not sure to write this, but maybe someone will save a few hours:

 mvn clean 

can do the job if your whole configuration is already perfect!

0
Mar 19 '19 at 12:46
source share

Gradle : v4.10.3

IDE : IntelliJ

I ran into this problem when using gradle to run my build and test. Copying applicationContext.xml everywhere didn't help. Even showing the full path, as shown below, did not help!

 context = new ClassPathXmlApplicationContext("C:\\...\\applicationContext.xml"); 

The solution (at least for gradle) is how gradle handles resources. For my gradle project, I laid out the workspace as defined at https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_project_layout

When starting a test using the default task set, gradle includes the "processTestResources" step, which looks for test resources in C: \ ..... \ src \ test \ resources (Gradle helpfully provides the full path).

Your .properties file and applicationContext.xml should be in this directory. If the resource directory is missing (as it was in my case), you need to create it and copy the file (s) there. After that, just specifying the file name worked just fine.

 context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
0
May 14 '19 at 16:03
source share



All Articles