Dead Simple: Override Spring Properties with Environment Variables
For some reason, this super handy trick has eluded me for years. Note I’m summarizing this from a guide: https://www.tutorialworks.com/spring-boot-kubernetes-override-properties/. Figured it was worth repeating.
Imagine you have a spring app with the following properties
foo
foo.bar
foo.barBaz
And you’d like to override these parameters at run time via env variables. Instead of defining special variable placeholders as is commonly suggested eg:
foo=${FOO_VAR:#{null}}
just set env. variables using the following convention
prop env_var
----------------------foo -> FOO
foo.bar -> FOO_BAR
foo.barBaz -> FOO_BAR_BAZ
foo.bar-baz -> FOO_BAR-BAZ
In other words, spring knows implicitly to look for env_variables of the same name as property with the .
and camelcase replaced by _
Hope this helps!