1

In my application.yaml properties file I have a variable defined as below -

service-account:
#   secret:  //commented in the yaml file, to indicate that it's used in app but read from ENV variable.

From this SOF post, I understand how . (dots) and - (dashes) are converted.
Now in the ENV variables file - I don't have anything like -

service-account.secret or service-account_secret or service_account_secret etc

instead what i have in the env (file) is - SERVICEACCOUNT_SECRET=xyz

Does spring boot match variable service-account.secret in props file with SERVICEACCOUNT_SECRET env variable.

Can someone confirm.

  • No, there wont be automatic matching. You will need to assign the ENV var to your property by using the syntax `secret: ${MY_ENV_VAR}` in your yaml – Tobi Sep 02 '22 at 15:14
  • @Tobi - i have tested it and you also should try. Automatic Matching is done. –  Sep 02 '22 at 15:18
  • Can you provide me any reference in the spring boot docs? I could not find any – Tobi Sep 02 '22 at 15:31
  • @Tobi - i tested it through code.. i am not aware of a reference that i can share. I would suggest that you too test it and share your findings here so it helps others. –  Sep 02 '22 at 15:42

1 Answers1

3

Does spring boot match variable service-account.secret in props file with SERVICEACCOUNT_SECRET env variable.

Yes. The canonical form of a property is all lower-case with - and . separators. service-account.secret is in the canonical form. To convert from the canonical form to an environment variable, Spring Boot does the following:

  • Replaces dots (.) with underscores (_)
  • Removes any dashes (-)
  • Converts to uppercase

Following these steps, service-account.secret becomes SERVICEACCOUNT_SECRET so you can use the SERVICEACCOUNT_SECRET environment variable to set the service-account.secret property.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • gr8.. that helps +1. A related Q - does ENV values take precedence over inline value in yaml. –  Sep 02 '22 at 15:40
  • 1
    Yes they do. See [this section of the reference documentation](https://docs.spring.io/spring-boot/docs/2.7.x/reference/html/features.html#features.external-config). – Andy Wilkinson Sep 02 '22 at 16:58