Spring - applicationContext.xml cannot be opened because it does not exist

Viewed 146434

I have a Spring MVC application and a problem with JUnit tests combined with the file applicationContext.xml.

In my JUnit test class I write:

final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
service = (TestServiceImpl) context.getBean("testServiceImpl");

The error I get is that aplicationContect.xml can not be found:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

But it exists in the WEB-INF folder.

So, what's wrong here? Why does the file not exist for the JUnit test?

24 Answers

This happens to me from time to time when using eclipse For some reason (eclipse bug??) the "excluded" parameter gets a value *.* (build path for my resources folder)

Just change the exclusion to none (see red rectangle vs green rectangle) I hope this helps someone in the future because it was very frustrating to find.

excluded

Click on the src/main/java folder and right click and create the xml file. If you create the application.xml file in a subpackage other than /, it will not work.

Know your structure is look like this

package/
|  subpackage/
   | Abc.java
   | Test.java

/application.xml

enter image description here

I was struggling since a couple of hours for this issue because i was putting that file under resources folder but it didn't help me, finally i realized my mistake. Put it directly under src/main/java.

For me, it worked by keeping file(applicationContext.xml) in the resources folder

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

enter image description here

My solution:
If you have no folder WEB-INF please put the file applicationContext.xml into the folder source(src). enter image description here


Then Java Project can read file applicationContext.xml -> getBean -> perform your business.

enter image description hereThe solution is to place the xml file in resources folder(src->main-> resources) and use this object creation new ClassPathXmlApplicationContext("applicationContext.xml");

enter image description here

Create a Directory at the bottom of main directory named resources. That solved my issue.

You actually need to understand the ApplicationContext. It is an interface and it will have different implementations based on configuration.

As you are using new ClassPathXmlApplicationContext("applicationContext.xml"); , kindly pay attention to initial right hand-side , it says ClassPathXmlApplicationContext , so the XML must be present in the class path. So drag your applicationContext.xml wherever it is to the src folder.

Gist: new ClassPathXmlApplicationContext as the name [ClassPathXml]will look for the xml file in the src folder of your project, so drag your xml file there only.

ClassPathXmlApplicationContext—Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources.

FileSystemXmlApplicationContext—Loads a context definition from an XML file in the file system.

XmlWebApplicationContext—Loads context definitions from an XML file contained within a web application.

just change the containing package of your applicationContext.xml file.
 applicationContext.xml must be in src package not in your project package.
 e.g. 
     src(main package)
         com.yourPackageName(package within src)
         classes etc.
     applicationContext.xml(within src but outside of yourPackage or we can say 
                            parallel to yourPackage name)

your-module/src/applicationContext.xml

val context = ClassPathXmlApplicationContext("applicationContext.xml")

Check the directory path, the default path is /src/ and not /.

GL

I got the same issue while working on a maven project, so I recreate the configuration file spring.xml in src/main/java and it worked for me.

I solved it by moving the file applicationContext.xml in an src folder and main folder. ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

Related