spring-context.xml的位置 [英] Location of spring-context.xml
问题描述
当我在tomcat上运行我的应用程序时,spring-context.xml文件位于
When I run my application on tomcat the spring-context.xml file is located at
/ WEB-inf / spring-context .xml
/WEB-inf/spring-context.xml
这没关系。但运行junit测试我必须提供我的spring-test-context.xml的位置,如下所示:
This is ok. But running a junit test I have to supply it with the location of my spring-test-context.xml like this:
@ContextConfiguration(locations={"classpath:/spring-test-context.xml"})
唯一如果文件位于
/src/spring-context.xml
/src/spring-context.xml
如何让我的应用程序在同一位置找到我的spring-context文件?那么它适用于junit睾丸并部署在tomcat上?
How can I get my application to find my spring-context files in the same location? So that it works with junit testes and deployed on tomcat?
我试过这个并且它给了我很多关于找不到任何豆类的错误,但它没有说出来找不到文件..
I tried this and it gave me alot of errors about not finding any beans, but it didn't say it couldn't find the file..
classpath:/WEB-INF/spring-test-context.xml
推荐答案
正如duffymo暗示的那样,Spring TestContext Framework(TCF)假设字符串默认情况下,位置在类路径中。有关详细信息,请参阅JavaDoc以获取 ContextConfiguration 。
As duffymo hinted at, the Spring TestContext Framework (TCF) assumes that string locations are in the classpath by default. For details, see the JavaDoc for ContextConfiguration.
但是,请注意,您还可以使用Spring的资源抽象指定文件系统中具有绝对路径或相对路径的资源(即,通过使用file:前缀)。您可以在JavaDoc中找到有关 AbstractContextLoader $ c $中的html#modifyLocations%28java.lang.Class,%20java.lang.String ...%29rel =noreferrer> modifyLocations()方法c>。
Note, however, that you can also specify resources in the file system with either an absolute or relative path using Spring's resource abstraction (i.e., by using the "file:" prefix). You can find details on that in the JavaDoc for the modifyLocations() method in Spring's AbstractContextLoader
.
例如,如果您的XML配置文件位于src / main / webapp / WEB-INF / spring-config中项目文件夹中的.xml
,您可以将位置指定为相对文件系统路径,如下所示:
So for example, if your XML configuration file is located in "src/main/webapp/WEB-INF/spring-config.xml"
in your project folder, you could specify the location as a relative file system path as follows:
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")
作为替代方案,您可以将Spring配置文件存储在类路径中(例如, src / main / resources
),然后通过您的类路径引用它们Spring MVC配置 - 例如:
As an alternative, you could store your Spring configuration files in the classpath (e.g., src/main/resources
) and then reference them via the classpath in your Spring MVC configuration -- for example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
使用这种方法,您的测试配置将如下所示(请注意表示该测试配置的前导斜杠资源位于类路径的根目录中):
With this approach, your test configuration would simply look like this (note the leading slash that denotes that the resource is in the root of the classpath):
@ContextConfiguration("/spring-config.xml")
您可能还会找到使用XML资源进行上下文配置参考手册中有用的部分。
You might also find the Context configuration with XML resources section of the reference manual useful.
问候,
Sam
(Spring TestContext Framework的作者)
这篇关于spring-context.xml的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!