简体   繁体   English

spring-context.xml的位置

[英]Location of spring-context.xml

When I run my application on tomcat the spring-context.xml file is located at 当我在tomcat上运行我的应用程序时,spring-context.xml文件位于

/WEB-inf/spring-context.xml /WEB-inf/spring-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: 但是运行一个junit测试,我必须为它提供spring-test-context.xml的位置,如下所示:

@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

How can I get my application to find my spring-context files in the same location? 我怎样才能让我的应用程序在同一位置找到我的spring-context文件? So that it works with junit testes and deployed on tomcat? 以便它与junit testes一起使用并部署在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.. 我尝试了此操作,但由于找不到任何bean,给了我很多错误,但没有说明找不到文件。

classpath:/WEB-INF/spring-test-context.xml

As duffymo hinted at, the Spring TestContext Framework (TCF) assumes that string locations are in the classpath by default. 正如duffymo所暗示的那样,Spring TestContext Framework(TCF)假定默认情况下字符串位置在类路径中。 For details, see the JavaDoc for ContextConfiguration . 有关详细信息,请参见ContextConfiguration的JavaDoc。

Note, however, that you can also specify resources in the file system with either an absolute or relative path using Spring's resource abstraction (ie, by using the "file:" prefix). 但是请注意,您还可以使用Spring的资源抽象(即,使用“ file:”前缀)在文件系统中使用绝对路径或相对路径指定资源。 You can find details on that in the JavaDoc for the modifyLocations() method in Spring's AbstractContextLoader . 您可以在JavaDoc中找到有关Spring的AbstractContextLoaderModifyLocations()方法的详细信息。

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: 因此,例如,如果您的XML配置文件位于项目文件夹中的"src/main/webapp/WEB-INF/spring-config.xml"中,则可以将该位置指定为相对文件系统路径,如下所示:

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")

As an alternative, you could store your Spring configuration files in the classpath (eg, src/main/resources ) and then reference them via the classpath in your Spring MVC configuration -- for example: 或者,您可以将Spring配置文件存储在类路径中(例如src/main/resources ),然后在您的Spring MVC配置中通过类路径引用它们-例如:

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

You might also find the Context configuration with XML resources section of the reference manual useful. 您可能还会发现参考手册的“ 带有XML资源上下文配置”部分很有用。

Regards, 问候,

Sam 山姆

(author of the Spring TestContext Framework) (Spring TestContext Framework的作者)

The problem is that /WEB-INF is not in the CLASSPATH for a web app. 问题是/ WEB-INF不在Web应用程序的CLASSPATH中。 However, /WEB-INF/classes is. 但是,/ WEB-INF / classes是。

Your problem with testing is that you aren't running in an app server, so WEB-INF/classes isn't part of the CLASSPATH by default. 测试的问题在于您没有在应用服务器中运行,因此默认情况下WEB-INF / classes不属于CLASSPATH。 I'd recommend setting up your tests so that either WEB-INF/classes is in the test CLASSPATH or use a relative or absolute file path to find them. 我建议您设置测试,以使WEB-INF / classs在测试CLASSPATH中,或者使用相对或绝对文件路径来查找它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM