Spring Bootでプロパティを環境変数から読むときの変換ルール

これは完全に自分用メモなのですが

Spring Bootでプロパティの値を環境変数から読むときの変数名の変換ルールは下記のとおり。

To convert a property name in the canonical-form to an environment variable name you can follow these rules:

  • Replace dots (.) with underscores (_).
  • Remove any dashes (-).
  • Convert to uppercase.

For example, the configuration property spring.main.log-startup-info would be an environment variable named SPRING_MAIN_LOGSTARTUPINFO.

Environment variables can also be used when binding to object lists. To bind to a List, the element number should be surrounded with underscores in the variable name.

For example, the configuration property my.service[0].other would use an environment variable named MY_SERVICE_0_OTHER.

docs.spring.io

tr コマンドで tr 'a-z.[' 'A-Z__' | tr -d ']-' とすれば変換できる。

$ echo 'spring.main.log-startup-info' | tr 'a-z.[' 'A-Z__' | tr -d ']-'
SPRING_MAIN_LOGSTARTUPINFO
$ echo 'my.service[0].other' | tr 'a-z.[' 'A-Z__' | tr -d ']-'
MY_SERVICE_0_OTHER

環境

  • Spring Boot 2.6.2