spring-context.xml 的位置 [英] Location of spring-context.xml

查看:42
本文介绍了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"})

唯一有效的方法是文件位于

The only way this works is if the file is located in

/src/spring-context.xml

/src/spring-context.xml

如何让我的应用程序在同一位置找到我的 spring-context 文件?以便它与junit testes一起使用并部署在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?

我试过了,它给了我很多关于找不到任何 bean 的错误,但它并没有说它找不到文件..

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) 假定字符串位置默认位于类路径中.有关详细信息,请参阅 上下文配置.

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:"前缀)使用绝对路径或相对路径指定文件系统中的资源.您可以在 modifyLocations() 方法.

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.

问候,

山姆

(Spring TestContext 框架的作者)

这篇关于spring-context.xml 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆