Spring - applicationContext.xml 无法打开,因为它不存在

发布于 2024-09-27 10:49:49 字数 636 浏览 5 评论 0原文

我有一个 Spring MVC 应用程序,并且与文件 applicationContext.xml 结合使用 JUnit 测试时遇到问题。

在我的 JUnit 测试类中,我写道:

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

我得到的错误是找不到 aplicationContect.xml:

org.springframework.beans.factory.BeanDefinitionStoreException:从类路径资源 [applicationContext.xml] 解析 XML 文档时发生 IOException;嵌套异常是 java.io.FileNotFoundException:类路径资源 [applicationContext.xml] 无法打开,因为它不存在

但它存在于WEB-INF文件夹中。

那么,这里出了什么问题呢?为什么 JUnit 测试的文件不存在?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

尐偏执 2024-10-04 10:49:49

您应该将 Spring 文件保存在另一个文件夹中,标记为“source”(就像“src”或“resources”)。

WEB-INF 不是源文件夹,因此它不会包含在类路径中(即 JUnit 不会在那里查找任何内容)。

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).

安人多梦 2024-10-04 10:49:49

如果您使用 maven,请在主目录中创建一个名为 resources 的目录,然后将 applicationContext.xml 复制到其中。

从你的java代码调用:

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

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");
十雾 2024-10-04 10:49:49

我遇到了同样的错误。
我解决了这个问题,将文件 applicationContext.xml 移动到

src 文件夹的子文件夹中。例如:

context = new ClassPathXmlApplicationContext("/com/ejemplo/dao/applicationContext.xml");

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");
恍梦境° 2024-10-04 10:49:49

ClassPathXmlApplicationContext 不会在您的 WEB-INF 文件夹中找到 applicationContext.xml,它不在类路径上。运行测试时,您可以将应用程序上下文复制到类路径中(可以将其放在 src/test/resources 下并让 Maven 复制它)。

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.

暖风昔人 2024-10-04 10:49:49

我也发现了这个问题。解决这个问题的方法是到处复制/粘贴这个文件并运行,一次一个文件。最后编译运行成功,然后删除不需要的。根据我的情况,正确的位置是:
输入图像描述这里

这是在 /src/ 路径下(我使用 Intellij Idea 作为 IDE)。其他java源文件位于/src/com/package/路径下

希望有帮助。

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.

飞烟轻若梦 2024-10-04 10:49:49

我在使用eclipse的时候经常会出现这种情况
由于某种原因(eclipse bug??)“排除”参数获取值*.*(我的资源文件夹的构建路径)

只需将排除更改为无(参见红色矩形与绿色矩形)
我希望这对将来的人有帮助,因为找到它非常令人沮丧。

排除

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

骑趴 2024-10-04 10:49:49

单击 src/main/java 文件夹并右键单击并创建 xml 文件。如果您在 / 之外的子包中创建 application.xml 文件,它将不起作用。

知道你的结构是这样的

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

/application.xml

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
把梦留给海 2024-10-04 10:49:49

在此处输入图像描述

我在这个问题上挣扎了几个小时,因为我把这个问题文件放在资源文件夹下,但这对我没有帮助,最后我意识到我的错误。
直接放在src/main/java下。

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.

无远思近则忧 2024-10-04 10:49:49

对我来说,它的工作原理是将文件(applicationContext.xml)保存在资源文件夹

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

在此处输入图像描述

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

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

enter image description here

蓬勃野心 2024-10-04 10:49:49

在此处输入图像描述

在主目录底部创建一个名为 resources 的目录。这解决了我的问题。

enter image description here

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

在风中等你 2024-10-04 10:49:49

我将 applicationContext.xml 放在 src/main/java 文件夹中并且它有效

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

枫林﹌晚霞¤ 2024-10-04 10:49:49

我解决了这个问题,将文件 spring-context.xml 移动到 src 文件夹中。
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

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

℉絮湮 2024-10-04 10:49:49

我通过将 applicationContext.xml 添加到 Maven 项目的 jar/target/test-classes 中来修复它。并使用

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

代替

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

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() ) )
挽清梦 2024-10-04 10:49:49

我的解决方案
如果您没有文件夹WEB-INF,请将文件applicationContext.xml放入文件夹source(src)中。 输入图片描述在这里

然后Java项目可以读取文件applicationContext.xml -> getBean ->开展您的业务。

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.

空袭的梦i 2024-10-04 10:49:49

在此处输入图片描述解决方案是将xml文件放在resources文件夹中(src-> main-> resources)并使用此对象创建 new ClassPathXmlApplicationContext("applicationContext.xml");

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

忆依然 2024-10-04 10:49:49

如果您使用 Intelij IDE 和 Maven,请确保 .xml 文件位于资源文件夹

确保 config.xml 位于资源文件夹中


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

帥小哥 2024-10-04 10:49:49

在 Spring 中,所有源文件都位于 src/main/java 中。
同样,资源通常保存在 src/main/resources 中。
因此,请将 spring 配置文件保存在 resources 文件夹中。

确保 src/main/resources 中也有文件的 ClassPath 条目。

在 .classpath 中检查以下 2 行。如果缺少,请添加它们。

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

因此,如果一切准备就绪,下面的代码应该可以工作。

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

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

淡看悲欢离合 2024-10-04 10:49:49

请执行此代码 - 它有效

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

:删除主方法类并在重新创建时重新创建它,请取消选中其有效的继承抽象方法

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

懵少女 2024-10-04 10:49:49

我正在使用 Netbeans,我通过将文件放入其他源默认包中解决了我的问题,然后我以这种方式调用它:

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

< a href="https://i.sstatic.net/OcD0j.png" rel="nofollow noreferrer">资源文件夹

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

神仙妹妹 2024-10-04 10:49:49

在使用 Maven 时遇到了同样的问题,然后我将 XML 文件放入 src/main/java 路径中并且它起作用了。

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

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");
此岸叶落 2024-10-04 10:49:49

您实际上需要了解 ApplicationContext。它是一个接口,根据配置会有不同的实现。

当您使用 new ClassPathXmlApplicationContext("applicationContext.xml"); 时,请注意初始右侧,它显示 ClassPathXmlApplicationContext ,因此 XML< /strong> 必须出现在类路径中。
因此,将 applicationContext.xml 拖到 src 文件夹中的任意位置。

要点:新的 ClassPathXmlApplicationContext 作为名称
[ClassPathXml]将在您的 src 文件夹中查找 xml 文件
项目,因此只需将 xml 文件拖到那里即可。

ClassPathXmlApplicationContext - 从 XML 加载上下文定义
文件位于类路径中,将上下文定义文件视为类路径
资源。

FileSystemXmlApplicationContext - 从 XML 加载上下文定义
文件系统中的文件。

XmlWebApplicationContext - 从包含的 XML 文件加载上下文定义
在 Web 应用程序中。

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.

少跟Wǒ拽 2024-10-04 10:49:49
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)
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)
财迷小姐 2024-10-04 10:49:49

your-module/src/applicationContext.xml

val context = ClassPathXmlApplicationContext("applicationContext.xml")

检查目录路径,默认路径是 /src/ 而不是 /

GL

your-module/src/applicationContext.xml

val context = ClassPathXmlApplicationContext("applicationContext.xml")

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

GL

探春 2024-10-04 10:49:49

我在处理 Maven 项目时遇到了同样的问题,因此我在 src/main/java 中重新创建了配置文件 spring.xml ,它对我有用。

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.

笛声青案梦长安 2024-10-04 10:49:49

我通过将文件 applicationContext.xml 移动到 src 文件夹和主文件夹中解决了这个问题。
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

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

完美的未来在梦里 2024-10-04 10:49:49

您可以使用“AnnotationConfigApplicationContext”而不是“ClassPathXmlApplicationContext”。

ApplicationContext context=new AnnotationConfigApplicationContext("config.xml");

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

ApplicationContext context=new AnnotationConfigApplicationContext("config.xml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文