Spring Boot应用程序的application.yml文件是否支持在键上引用变量?

huangapple 未分类评论70阅读模式
英文:

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会有两个键:10011005,但实际值是SETTINGS_ID_1SETTINGS_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&#39;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&#39;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}]
    &#39;[${SETTINGS_ID_2}]&#39;
    $SETTINGS_ID_1
    &#39;$SETTINGS_ID_1&#39;


spring-boot version `2.2.2` (I guess it does not depend on the version, but anyway provided)


</details>


huangapple
  • 本文由 发表于 2020年5月29日 22:30:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/62088285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定