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

26

52

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?

Smallage answered 11/10, 2010 at 15:55 Comment(0)
S
52

You should keep your Spring files in another folder, marked as "source" (just like "src" or "resources").

WEB-INF is not a source folder, therefore it will not be included in the classpath (i.e. JUnit will not look for anything there).

Superscribe answered 11/10, 2010 at 16:2 Comment(0)
C
49

If you use maven, create a directory called resources in the main directory, and then copy your applicationContext.xml into it.

From your java code call:

ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
Cony answered 15/8, 2014 at 14:31 Comment(4)
Thanks, I use maven and putting applicationContext.xml into resources solved my problem right away!Conceptualize
Thanks a lot. Also, none of the other solutions mentioned here work for maven project in IntelliJPneumococcus
How to change this behavior?Mccue
@duracell, thanks a lot. I have been constantly refactoring my applicationContext.xml to various locations but nothing worked. Your answer helped meUtley
B
10

I got the same error. I solved it moving the file applicationContext.xmlin a

sub-folder of the srcfolder. e.g:

context = new ClassPathXmlApplicationContext("/com/ejemplo/dao/applicationContext.xml");
Burgle answered 12/10, 2012 at 2:47 Comment(1)
had is in src had to include the path like aboveMoffatt
H
8

The ClassPathXmlApplicationContext isn't going to find the applicationContext.xml in your WEB-INF folder, it's not on the classpath. You could copy the application context into your classpath (could put it under src/test/resources and let Maven copy it over) when running the tests.

Hennessey answered 11/10, 2010 at 15:58 Comment(0)
C
7

I also found this problem. What do did to solve this is to copy/paste this file everywhere and run, one file a time. Finally it compiled and ran successfully, and then delete the unnecessary ones. The correct place in my situation is: enter image description here

This is under the /src/ path (I am using Intellij Idea as the IDE). The other java source files are under /src/com/package/ path

Hope it helpes.

Crenelate answered 7/3, 2016 at 21:13 Comment(0)
V
4

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

Vindicate answered 30/1, 2018 at 21:28 Comment(0)
D
2

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
Drumfire answered 30/4, 2018 at 11:26 Comment(0)
G
2

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.

Grussing answered 7/8, 2020 at 11:18 Comment(0)
U
2

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

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

enter image description here

Usurpation answered 4/3, 2022 at 7:24 Comment(0)
P
2

enter image description here

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

Pankey answered 31/7, 2022 at 7:44 Comment(0)
F
1

I placed the applicationContext.xml in the src/main/java folder and it worked

Ferdinande answered 26/5, 2014 at 16:33 Comment(0)
C
1

I solved it moving the file spring-context.xml in a src folder. ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

Communicative answered 9/8, 2016 at 20:27 Comment(0)
E
1

I fixed it by adding applicationContext.xml into jar/target/test-classes for Maven project. And use

        XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
                "/applicationContext.xml", getClass() ) )

instead of

        XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
                "/WEB-INF/applicationContext.xml", getClass() ) )
Ellyellyn answered 30/3, 2018 at 21:25 Comment(0)
S
1

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.

Stockjobber answered 4/5, 2021 at 10:6 Comment(0)
L
1

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

Loadstar answered 26/7, 2021 at 16:42 Comment(0)
I
1

If You are Working in Intelij IDE and Maven then Make sure that the .xml file is in the resources folder

Make Sure that config.xml is in the resources Folder

Inflict answered 7/10, 2023 at 16:52 Comment(0)
P
0

In Spring all source files are inside src/main/java. Similarly, the resources are generally kept inside src/main/resources. So keep your spring configuration file inside resources folder.

Make sure you have the ClassPath entry for your files inside src/main/resources as well.

In .classpath check for 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 below code should work.

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

Purington answered 1/3, 2017 at 20:26 Comment(0)
O
0

Please do This code - it worked

 AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");

o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked

Oldest answered 2/3, 2017 at 5:51 Comment(0)
B
0

I'm using Netbeans, i solved my problem by putting the file in: Other Sources default package, then i called it in this way:

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

resources folder

Becki answered 30/3, 2018 at 16:39 Comment(0)
M
0

While working with Maven got same issue then I put XML file into src/main/java path and it worked.

ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Microscopy answered 17/4, 2018 at 4:7 Comment(0)
G
0

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.

Greenfinch answered 13/12, 2018 at 6:28 Comment(0)
C
0
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)
Curley answered 2/4, 2019 at 20:2 Comment(0)
T
0

your-module/src/applicationContext.xml

val context = ClassPathXmlApplicationContext("applicationContext.xml")

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

GL

Trenatrenail answered 13/2, 2020 at 4:9 Comment(0)
E
0

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.

Entozoic answered 3/2, 2021 at 6:1 Comment(0)
S
0

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

Santiagosantillan answered 19/5, 2022 at 11:44 Comment(0)
R
0

Your can use "AnnotationConfigApplicationContext" instead of "ClassPathXmlApplicationContext".

ApplicationContext context=new AnnotationConfigApplicationContext("config.xml");
Raguelragweed answered 7/1 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.