@TestPropertySource 会尊重 SPEL 或其他属性的值吗?

Will @TestPropertySource honor SPEL or values from other properties?

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

这是一个与

类似的问题

假设我所指的属性存在于 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 才能完成此操作?

直接来自 @TestPropertySource.locations():

的 Javadoc

Property placeholders in paths (i.e., ${...}) will be resolved against the Environment.

... 这意味着:针对已经添加到 Environment.

的任何内容

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

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

此致,

Sam (Spring TestContext 框架的作者)