Java 使用系统属性作为@WebAppConfiguration的值

Java 使用系统属性作为@WebAppConfiguration的值,java,spring-mvc,spring-test,spring-test-mvc,Java,Spring Mvc,Spring Test,Spring Test Mvc,是否可以对@WebappConfiguration注释的值使用系统属性?我试过这样的方法: @WebAppConfiguration("#{systemProperties['webapproot'] ?: 'war'}") 但它似乎根本不起作用。有没有其他方法可以让你度过春天?我不想通过我们的构建工具来实现这一点,因为它会破坏从IDE执行集成测试的过程。@WebAppConfiguration似乎既不支持SpEL也不支持占位符解析 我用以下方法检查它:我尝试注入系统属性并使用@Value解析

是否可以对@WebappConfiguration注释的值使用系统属性?我试过这样的方法:

@WebAppConfiguration("#{systemProperties['webapproot'] ?: 'war'}")

但它似乎根本不起作用。有没有其他方法可以让你度过春天?我不想通过我们的构建工具来实现这一点,因为它会破坏从IDE执行集成测试的过程。

@WebAppConfiguration
似乎既不支持SpEL也不支持占位符解析


我用以下方法检查它:我尝试注入系统属性并使用
@Value
解析占位符。当我尝试注入一个不存在的属性
@Value
时,抛出
SpelEvaluationException:EL1008E:在类型为“java.util.Properties”的对象上找不到属性或字段“asd”
IllegalArgumentException:无法解析值“${nonexistent\u property}”中的占位符“nonexistent\u property”
@WebAppConfiguration
只是
{systemProperties.asd}
${nonexistent\u property}
初始化为简单的
字符串。

@WebAppConfiguration
似乎既不支持SpEL也不支持占位符解析


我用以下方法检查它:我尝试注入系统属性并使用
@Value
解析占位符。当我尝试注入一个不存在的属性
@Value
时,抛出
SpelEvaluationException:EL1008E:在类型为“java.util.Properties”的对象上找不到属性或字段“asd”
IllegalArgumentException:无法解析值“${nonexistent\u property}”中的占位符“nonexistent\u property”
@WebAppConfiguration
只是
{systemProperties.asd}
${nonexistent\u property}
初始化为简单的
字符串。

否,遗憾的是不支持

提供给
@WebAppConfiguration
值必须是类级JavaDoc中规定的显式资源路径

如果您想让我们考虑<代码>值>代码>属性的动态评价,请随意打开一个请求这样的支持。 问候,


Sam(SpringTestContext框架的作者)

不,不幸的是,它不受支持

提供给
@WebAppConfiguration
值必须是类级JavaDoc中规定的显式资源路径

如果您想让我们考虑<代码>值>代码>属性的动态评价,请随意打开一个请求这样的支持。 问候,


Sam(SpringTestContext框架的作者)

我找到了这个问题的解决方案。我通过扩展Spring用来加载
WebAppConfiguration
-Annotation值的
WebTestContextBootstrapper
编写了自己的
contextbootstrapper

我扩展了该功能,包括检查某个系统属性是否存在,如果存在,则覆盖注释值:

 public class SystemPropTestContextBootstrapper extends WebTestContextBootstrapper {

    @Override
    protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
        WebAppConfiguration webAppConfiguration = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
                WebAppConfiguration.class);
        if (webAppConfiguration != null) {
            //implementation ommited
            String webappDir = loadWebappDirFromSystemProperty();
            if(webappDir == null) {
                webappDir = webAppConfiguration.value();
            }
            return new WebMergedContextConfiguration(mergedConfig, webappDir);
        }

        return mergedConfig;
    }
}
然后,该类可以与
@BootstrapWith
-注释一起使用:

@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(SystemPropTestContextBootstrapper.class)
@WebAppConfiguration("standardDir")
public class SomeTest {

}

此解决方案使我能够从构建工具运行测试,同时保持从IDE运行测试的能力,这非常好。

我找到了解决此问题的方法。我通过扩展Spring用来加载
WebAppConfiguration
-Annotation值的
WebTestContextBootstrapper
编写了自己的
contextbootstrapper

我扩展了该功能,包括检查某个系统属性是否存在,如果存在,则覆盖注释值:

 public class SystemPropTestContextBootstrapper extends WebTestContextBootstrapper {

    @Override
    protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
        WebAppConfiguration webAppConfiguration = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
                WebAppConfiguration.class);
        if (webAppConfiguration != null) {
            //implementation ommited
            String webappDir = loadWebappDirFromSystemProperty();
            if(webappDir == null) {
                webappDir = webAppConfiguration.value();
            }
            return new WebMergedContextConfiguration(mergedConfig, webappDir);
        }

        return mergedConfig;
    }
}
然后,该类可以与
@BootstrapWith
-注释一起使用:

@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(SystemPropTestContextBootstrapper.class)
@WebAppConfiguration("standardDir")
public class SomeTest {

}
此解决方案使我能够从构建工具运行测试,同时保持从IDE运行测试的能力,这非常好。

使用$代替#不起作用。

使用$代替#也不起作用