java 无法处理用于上下文配置的位置和类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27979735/
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
Cannot process locations AND classes for context configuration
提问by gstackoverflow
I have wrote following test:
我写了以下测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/dataContext.xml"},classes = Configiuration.class)
@ActiveProfiles("test")
public class CityDaoImplTest {
....
}
I need to use configuration from xml file and from java class bur when I invoke
当我调用时,我需要使用来自 xml 文件和 java 类 bur 的配置
mvn test I seee following in console:
mvn test 我在控制台中看到以下内容:
Tests in error:
initializationError(***.CityDaoImplTest): Cannot process locations AND classes for context configuration [ContextConfigurationAttributes@5bb21b69 declaringClass = '***.CityDaoImplTest', classes = '{***.Configiuration}', locations = '{classpath:META-INF/dataContext.xml}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']; configure one or the other, but not both.
How to fix it without rewriting configuration?
如何在不重写配置的情况下修复它?
回答by wassgren
From the Spring Docs:
来自Spring 文档:
Prior to Spring 3.1, only path-based resource locations were supported. As of Spring 3.1, context loaders may choose to support eitherpath-based orclass-based resources. As of Spring 4.0.4, context loaders may choose to support path-based andclass-based resources simultaneously.
在 Spring 3.1 之前,仅支持基于路径的资源位置。在Spring 3.1中,背景装载机可以选择支持任何基于路径或基于类的资源。在Spring 4.0.4中,上下文装载机可以选择支持基于路径和同时基于类的资源。
However, with spring-test there is a small caveat. It uses the SmartContextLoader
which is based on AbstractDelegatingSmartContextLoader
and unfortunately it is not so smart ;)
但是,对于弹簧测试,有一个小警告。它使用SmartContextLoader
基于的AbstractDelegatingSmartContextLoader
,不幸的是它不是那么聪明;)
@Override
public void processContextConfiguration(
final ContextConfigurationAttributes configAttributes) {
Assert.notNull(configAttributes, "configAttributes must not be null");
Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format(
"Cannot process locations AND classes for context "
+ "configuration %s; configure one or the other, but not both.", configAttributes));
As shown in the code, locations and classes can not both be set.
如代码所示,位置和类不能同时设置。
So, how to fix this? Well, one solution is to add an extra config class such as the following:
那么,如何解决这个问题?嗯,一种解决方案是添加一个额外的配置类,如下所示:
@Configuration
@ImportResource("classpath:META-INF/dataContext.xml")
class TestConfig {
}
And, in your test code use the following:
并且,在您的测试代码中使用以下内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Configuration.class, TestConfig.class})
@ActiveProfiles("test")
public class CityDaoImplTest { ... }
Technically, this is rewriting the configurationbut you do not have to alter your existing config, just add a new @Configuration
class (and that class can even be in the same file as your test case).
从技术上讲,这是重写配置,但您不必更改现有配置,只需添加一个新@Configuration
类(该类甚至可以与您的测试用例在同一个文件中)。
回答by Aditzu
Even if is to late for you I will post my answer just to help the other ones which will read this.
即使对您来说为时已晚,我也会发布我的答案,以帮助其他会阅读本文的人。
Another solution is to declare your Configuration class as bean in dataContext.xml.
另一种解决方案是在 dataContext.xml 中将您的 Configuration 类声明为 bean。
All you need to do is:
您需要做的就是:
<bean class="com.packageWhereConfigClassIsPresent.Configuration"/>
Hope it will help someone ;)
希望它会帮助某人;)