英文:
Does Spring Boot application.yml support referencing to variables at key
问题
我需要在Spring Boot的`application.yml`文件中,将map的键设置为占位符,这些占位符引用了一些现有变量(例如环境变量/每个配置文件中声明的变量)。
SETTINGS_ID_1: 1001
SETTINGS_ID_2: 1005
settingsMapping:
${SETTINGS_ID_1}: hello
${SETTINGS_ID_2}: hi
因此,我期望settingsMapping
会有两个键:1001
和1005
,但实际值是SETTINGS_ID_1
和SETTINGS_ID_2
。
在application.yml
中,Spring是否支持这样的特性呢?这些占位符在map的值中可以正常工作,但在键中却不行。
我尝试了以下选项,但都没有成功(属性要么按原样保留为字符串值,要么抛出异常ParserException: while parsing a flow sequence
):
- [${SETTINGS_ID_2}]
- '[${SETTINGS_ID_2}]'
- $SETTINGS_ID_1
- '$SETTINGS_ID_1'
Spring Boot版本为2.2.2
(我猜这与版本无关,但还是提供了版本信息)。
<details>
<summary>英文:</summary>
I need to have placeholders at map's keys inside Spring Boot `application.yml` that referencing to some existing variables (e.g. env variables / variables declared in props per profile)
SETTINGS_ID_1: 1001
SETTINGS_ID_2: 1005
settingsMapping:
${SETTINGS_ID_1}: hello
${SETTINGS_ID_2}: hi
So I expect that `settingsMapping` will have two keys: `1001` and `1005`, but real values are `SETTINGS_ID_1` and `SETTINGS_ID_2`.
Does Spring support such feature at `application.yml`? These placeholders work fine in map's values, but not in keys.
I tried the following options but without luck (props either have string values as is it, or exception `ParserException: while parsing a flow sequence`):
[${SETTINGS_ID_2}]
'[${SETTINGS_ID_2}]'
$SETTINGS_ID_1
'$SETTINGS_ID_1'
spring-boot version `2.2.2` (I guess it does not depend on the version, but anyway provided)
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论