Spring - applicationContext.xml 无法打开,因为它不存在
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
您应该将 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).
如果您使用 maven,请在主目录中创建一个名为
resources
的目录,然后将applicationContext.xml
复制到其中。从你的java代码调用:
If you use maven, create a directory called
resources
in the main directory, and then copy yourapplicationContext.xml
into it.From your java code call:
我遇到了同样的错误。
我解决了这个问题,将文件
applicationContext.xml
移动到src
文件夹的子文件夹中。例如:I got the same error.
I solved it moving the file
applicationContext.xml
in asub-folder of the
src
folder. e.g:ClassPathXmlApplicationContext
不会在您的WEB-INF
文件夹中找到applicationContext.xml
,它不在类路径上。运行测试时,您可以将应用程序上下文复制到类路径中(可以将其放在 src/test/resources 下并让 Maven 复制它)。The
ClassPathXmlApplicationContext
isn't going to find theapplicationContext.xml
in yourWEB-INF
folder, it's not on the classpath. You could copy the application context into your classpath (could put it undersrc/test/resources
and let Maven copy it over) when running the tests.我也发现了这个问题。解决这个问题的方法是到处复制/粘贴这个文件并运行,一次一个文件。最后编译运行成功,然后删除不需要的。根据我的情况,正确的位置是:
这是在 /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:
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.
我在使用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.
单击 src/main/java 文件夹并右键单击并创建 xml 文件。如果您在
/
之外的子包中创建application.xml
文件,它将不起作用。知道你的结构是这样的
Click on the
src/main/java
folder and right click and create the xml file. If you create theapplication.xml
file in a subpackage other than/
, it will not work.Know your structure is look like this
在此处输入图像描述
我在这个问题上挣扎了几个小时,因为我把这个问题文件放在资源文件夹下,但这对我没有帮助,最后我意识到我的错误。
直接放在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.
对我来说,它的工作原理是将文件(applicationContext.xml)保存在资源文件夹
For me, it worked by keeping file(applicationContext.xml) in the resources folder
在主目录底部创建一个名为 resources 的目录。这解决了我的问题。
Create a Directory at the bottom of main directory named resources. That solved my issue.
我将 applicationContext.xml 放在 src/main/java 文件夹中并且它有效
I placed the applicationContext.xml in the src/main/java folder and it worked
我解决了这个问题,将文件 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");
我通过将 applicationContext.xml 添加到 Maven 项目的 jar/target/test-classes 中来修复它。并使用
代替
I fixed it by adding applicationContext.xml into jar/target/test-classes for Maven project. And use
instead of
我的解决方案:
如果您没有文件夹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).
Then Java Project can read file applicationContext.xml -> getBean -> perform your business.
在此处输入图片描述解决方案是将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");
如果您使用 Intelij IDE 和 Maven,请确保 .xml 文件位于资源文件夹
If You are Working in Intelij IDE and Maven then Make sure that the .xml file is in the resources folder
在 Spring 中,所有源文件都位于 src/main/java 中。
同样,资源通常保存在 src/main/resources 中。
因此,请将 spring 配置文件保存在 resources 文件夹中。
确保 src/main/resources 中也有文件的 ClassPath 条目。
在 .classpath 中检查以下 2 行。如果缺少,请添加它们。
因此,如果一切准备就绪,下面的代码应该可以工作。
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.
So, if you have everything in place the below code should work.
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");
请执行此代码 - 它有效
:删除主方法类并在重新创建时重新创建它,请取消选中其有效的继承抽象方法
Please do This code - it worked
o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked
我正在使用 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
在使用 Maven 时遇到了同样的问题,然后我将 XML 文件放入 src/main/java 路径中并且它起作用了。
While working with Maven got same issue then I put XML file into
src/main/java
path and it worked.您实际上需要了解
ApplicationContext
。它是一个接口,根据配置会有不同的实现。当您使用
new ClassPathXmlApplicationContext("applicationContext.xml");
时,请注意初始右侧,它显示 ClassPathXmlApplicationContext ,因此 XML< /strong> 必须出现在类路径中。因此,将 applicationContext.xml 拖到 src 文件夹中的任意位置。
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.
ClassPathXmlApplicationContext
—Loads a context definition from an XMLfile located in the classpath, treating context definition files as classpath
resources.
FileSystemXmlApplicationContext
—Loads a context definition from an XMLfile in the file system.
XmlWebApplicationContext
—Loads context definitions from an XML file containedwithin a web application.
your-module/src/applicationContext.xml
检查目录路径,默认路径是
/src/
而不是/
。GL
your-module/src/applicationContext.xml
Check the directory path, the default path is
/src/
and not/
.GL
我在处理 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
insrc/main/java
and it worked for me.我通过将文件 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");
您可以使用“AnnotationConfigApplicationContext”而不是“ClassPathXmlApplicationContext”。
Your can use "AnnotationConfigApplicationContext" instead of "ClassPathXmlApplicationContext".