spring Spring上下文测试找不到配置位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1009357/
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 context tests can't find config locations
提问by Adam B
I have a large application spread across multiple Spring bean definition xml files. In my test suite I manually load up the XML files I need using a FileSystemXmlApplicationContext to perform the tests I want to run. This reduces test set up time and allows me to use the same exact configuration files that are used in production.
我有一个跨多个 Spring bean 定义 xml 文件的大型应用程序。在我的测试套件中,我使用 FileSystemXmlApplicationContext 手动加载我需要的 XML 文件来执行我想要运行的测试。这减少了测试设置时间,并允许我使用与生产中使用的完全相同的配置文件。
Now I'm trying to use Spring's transactional test base classes which take the config locations and load the context for me. For some reason when the application context is created Spring cannot find any of the config files. This is confusing because I run the test from the same working directory as when I load the config myself using FileSystemXmlApplicationContext. If I prepend all my config locations with "file:" the paths I specify in my test are found, but any files that are imported or referenced by beans defined in the config (e.g. properties files) cannot be found. What's the deal? Can I get tests that extend the spring context test classes to work the same as the ones where I create the context myself?
现在我正在尝试使用 Spring 的事务测试基类,它获取配置位置并为我加载上下文。出于某种原因,在创建应用程序上下文时,Spring 找不到任何配置文件。这令人困惑,因为我从与使用 FileSystemXmlApplicationContext 自己加载配置时相同的工作目录运行测试。如果我在所有配置位置前面加上“file:”,我在测试中指定的路径会被找到,但是任何由配置中定义的 bean 导入或引用的文件(例如属性文件)都找不到。这是怎么回事?我可以获得扩展 spring 上下文测试类的测试,使其与我自己创建上下文的测试相同吗?
For example, creating the context like this works fine:
例如,创建这样的上下文工作正常:
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "WEB-INF/services-context.xml"})
If I extend AbstractTransactionalDataSourceSpringContextTests the following does not find services-context.xml:
如果我扩展 AbstractTransactionalDataSourceSpringContextTests,则以下内容找不到 services-context.xml:
@Override
protected String[] getConfigLocations() {
return new String[] { "WEB-INF/services-context.xml"};
}
This finds services-context, but the PropertyPlaceholderConfigurer defined in there fails to find it's properties files.
这会找到 services-context,但其中定义的 PropertyPlaceholderConfigurer 找不到它的属性文件。
@Override
protected String[] getConfigLocations() {
return new String[] { "file:WEB-INF/services-context.xml"};
}
采纳答案by Adam B
In addition to overriding getConfigLocations I also overrode loadContext and used a trusty fileSystemXmlApplicationContext in there.
除了覆盖 getConfigLocations 之外,我还覆盖了 loadContext 并在其中使用了一个可靠的 fileSystemXmlApplicationContext 。
@Override
protected String[] getConfigLocations() {
return new String[] { "WEB-INF/services-config.xml" };
}
@Override
protected ConfigurableApplicationContext loadContext(String[] locations) throws Exception {
return new FileSystemXmlApplicationContext(locations);
}
回答by paulcm
We put all of our Spring config and properties files in the classpath, which keeps things simple - we can just extend our test classes from a base class like:
我们将所有 Spring 配置和属性文件放在类路径中,这使事情变得简单 - 我们可以从基类扩展我们的测试类,例如:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"/spring/*.xml",
"/testSpring/*.xml" })
public abstract class AbstractIntegrationTest {
Here, the paths are all paths in classpath.
这里的路径都是类路径中的路径。
If you don't want to do that, have you checked how you are referencing the properties files in your services-context.xml? I suspect that if you add file: to your context configuration, then you'll also need to add this to your property file reference. You could perhaps just use a separate test Spring config file to change the definition of your property placeholder, and place this at the end of your list of context files - its definitions will then override those defined in earlier files.
如果您不想这样做,您是否检查过如何引用 services-context.xml 中的属性文件?我怀疑如果您将 file: 添加到您的上下文配置中,那么您还需要将其添加到您的属性文件引用中。您也许可以使用单独的测试 Spring 配置文件来更改属性占位符的定义,并将其放在上下文文件列表的末尾 - 然后它的定义将覆盖先前文件中定义的那些。
回答by skaffman
Your config locations are relative URIs, and will be interpreted as such by the base test class, with the URI being resolved relative to the location of the test class itself. Try using fully qualified URIs, or use relative URI taking into account where the test class is.
您的配置位置是相对 URI,并且将由基本测试类解释为相对 URI,URI 将相对于测试类本身的位置进行解析。尝试使用完全限定的 URI,或使用相对 URI 考虑测试类的位置。
回答by Alex
Can't you use classpath XML factories like ClassPathXmlApplicationContext?
您不能使用ClassPathXmlApplicationContext 之类的类路径 XML 工厂吗?
回答by zawhtut
Another possible solution is to duplicate the services-config.xmland rename as services-config-test.xmland then put under classpath. The same thing goes for properties file.
另一种可能的解决方案是复制services-config.xml并重命名为services-config-test.xml,然后放在类路径下。属性文件也是如此。
回答by Mr Lou
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "WebRoot/WEB-INF/services-context.xml"})

