【问题标题】:Will @TestPropertySource honor SPEL or values from other properties?@TestPropertySource 会尊重 SPEL 或其他属性的值吗?
【发布时间】:2016-10-26 11:25:46
【问题描述】:

我想知道@TestPropertySource 是否会支持SpEL,或者至少允许一个属性替换另一个属性的值。

@TestPropertySource with dynamic properties类似的问题

假设我所指的属性存在于locations 属性中的一个文件中......

例如,如果我想做类似的事情:

@TestPropertySource(
    locations = {"classpath:application.properties", "classpath:database.properties"},
    properties = {"newPortNum = #{1 + Integer.parseInt(${myapp.web.server.port.ssl})}})

或者这个:

@TestPropertySource(
    locations = {"classpath:application.properties", "classpath:database.properties"},
    properties = {"outputFile = ${outputDir}/foo.txt"})

我需要实现TestExecutionListener@BootstrapWith 来完成此操作吗?

【问题讨论】:

    标签: spring spring-boot spring-test


    【解决方案1】:

    直接来自 @TestPropertySource.locations() 的 Javadoc:

    路径中的属性占位符(即${...})将针对Environment进行解析。

    ...这意味着:针对已添加到 Environment 的任何内容。

    另一方面,SpEL 表达式不受支持。

    如果您需要将PropertySource 添加到Environment 的编程支持,您应该实现一个ApplicationContextInitializer,它可以通过@ContextConfiguration(initializers = ...) 注册。

    问候,

    Sam (Spring TestContext 框架的作者)

    【讨论】:

    • 我已经确认您的两个建议都有效。谢谢
    猜你喜欢
    • 2017-01-04
    • 2015-08-28
    • 1970-01-01
    • 2016-02-24
    • 2016-01-27
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多