eclipse Spring ContextLoader 无法找到 applicationContext.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2191123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring ContextLoader fails to find applicationContext.xml
提问by Lunikon
I've been working alone on a project for a long time and now another developer joined so I wanted to set up the project on his machine from SVN. We are both using very similar set-ups:
我已经在一个项目上独自工作了很长时间,现在另一个开发人员加入了,所以我想在他的机器上从 SVN 设置这个项目。我们都使用非常相似的设置:
- OS X Snow Leopard
- Latest Eclipse WTP
- m2eclipse Maven Plugin
- Subversive SVN Plugin
- Tomcat 6.0.24
- OS X 雪豹
- 最新的 Eclipse WTP
- m2eclipse Maven 插件
- 颠覆性的SVN插件
- 雄猫 6.0.24
The Spring version used is 2.5.6.
使用的 Spring 版本是 2.5.6。
I set up the project on his machine using Eclipse's "Project from SVN" tool, essentially creating a 1:1 copy of the project. Maven took care of all dependencies without problems as far as I can tell, the project compiled without problems. The project structure is following the Maven standard for webapps with resources (like the applicationContext.xml) in src/main/resources, the project's code in src/main/java and all web-app related files in src/main/webapp and src/main/webapp/WEB-INF respectively.
我使用 Eclipse 的“来自 SVN 的项目”工具在他的机器上设置了项目,基本上创建了项目的 1:1 副本。据我所知,Maven 毫无问题地处理了所有依赖项,项目编译没有问题。项目结构遵循 webapps 的 Maven 标准,在 src/main/resources 中包含资源(如 applicationContext.xml),在 src/main/java 中的项目代码以及在 src/main/webapp 和 src 中的所有 web-app 相关文件/main/webapp/WEB-INF 分别。
The web.xml is configured to load the context like so:
web.xml 配置为加载上下文,如下所示:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Using the "Servers" tool of Eclipse I tried launching the application right from Eclipse, but it failed immediately with a FileNotFoundException when trying to load the applicationContext.xml.
使用 Eclipse 的“服务器”工具,我尝试直接从 Eclipse 启动应用程序,但在尝试加载 applicationContext.xml 时它立即失败并出现 FileNotFoundException。
Since the code checked out from SVN has been left untouched I guess the problem has to do with some parameter for the Maven plugin. Or that I forgot some important step to finalize the set-up. Has anybody encountered a similar issue or has an idea where I should start looking?
由于从 SVN 检出的代码没有受到影响,我猜这个问题与 Maven 插件的某些参数有关。或者我忘记了完成设置的一些重要步骤。有没有人遇到过类似的问题或知道我应该从哪里开始寻找?
Thank you very much for you help!
非常感谢您的帮助!
回答by Pascal Thivent
The project structure is following the Maven standard for webapps with resources (like the applicationContext.xml) in src/main/resources, the project's code in src/main/java and all web-app related files in src/main/webapp and src/main/webapp/WEB-INF respectively.
项目结构遵循 webapps 的 Maven 标准,在 src/main/resources 中包含资源(如 applicationContext.xml),在 src/main/java 中的项目代码以及在 src/main/webapp 和 src 中的所有 web-app 相关文件/main/webapp/WEB-INF 分别。
This is correct and perfectly fine (as you want the applicationContext.xml
to be on the tests class path).
这是正确的并且非常好(因为您希望它applicationContext.xml
位于测试类路径上)。
It is not copied to WEB-INF/classes, but that's not the case in my own (functioning) project. Instead, the file is copied to target/classes as per Maven-standard.
它不会复制到 WEB-INF/classes,但在我自己的(正在运行的)项目中并非如此。相反,根据 Maven 标准将文件复制到目标/类。
It iscopied to target/yourapp/WEB-INF/classes
, but not by process-resources
which is called by default by m2eclipse on resource changes (this goal copies/filters resources to target/classes
). This happens during the package
phase (or when invoking explicitly war:war
, war:inplace
or war:exploded
). As explained in the usagepage, the WAR Plugin is not responsible for compiling the java sources or copying the resources.
它被复制到target/yourapp/WEB-INF/classes
,但不会被process-resources
m2eclipse 在资源更改时默认调用(此目标将资源复制/过滤到target/classes
)。这发生在package
阶段(或显式调用war:war
,war:inplace
或 时war:exploded
)。如用法页面所述,WAR 插件不负责编译 java 源代码或复制资源。
So I'd suggest to package
the war before to run it on a Server. Maybe you can configure m2eclipse to do this automatically on resource changes (right-clickon your project, then select Properties > Mavenand add the appropriate war
goal to the Goals to invoke on resource changes). Didn't try this though.
所以我建议package
在War之前在服务器上运行它。也许您可以将 m2eclipse 配置为在资源更改时自动执行此操作(右键单击您的项目,然后选择Properties > Maven并将适当的war
目标添加到目标以调用资源更改)。不过没试过这个。
回答by Bozho
When launching the application with the servers plugin, it expects that your web folder contains everything needed. That's why you have to configure your project output folder (from the project build path options) to be src/main/webapp/WEB-INF/classes
使用服务器插件启动应用程序时,它希望您的 Web 文件夹包含所需的一切。这就是为什么您必须将项目输出文件夹(来自项目构建路径选项)配置为src/main/webapp/WEB-INF/classes
Alternatively you can use the FileSyncplugin to copy the files.
或者,您可以使用FileSync插件来复制文件。
Btw, you didn't specify what are the options for the Servers plugin, and whether your project is a "Dynamic Web Project" (i.e. deployable) or not.
顺便说一句,您没有指定服务器插件的选项是什么,以及您的项目是否是“动态 Web 项目”(即可部署)。