I want to override ANY property in application.properties with an ENV setting. In my application, I define properties using "prefix" with DOTs "." and suffix with "dash" (e.g., "-").
For example:
application.server.jgroups-port= some port #
Now, I want to override this property from the OS ENV settings.
On Windows, when I set this ENV property, this is my results:
First (FAILS),
Windows ENV >> APPLICATION_SERVER_JGROUPS_PORT = 5445
environment.getProperty("application.server.jgroups-port") returns NULL
Second (FAILS),
Windows ENV >> APPLICATION_SERVER_JGROUPSPORT = 5445
environment.getProperty("application.server.jgroups-port") returns NULL
Third (THIS WORKS!),
Windows ENV >> APPLICATION_SERVER_JGROUPS-PORT = 5445
environment.getProperty("application.server.jgroups-port") returns 5445
Notice, the "dash" (e.g., "-") on the last one.
YAY! I have effectively set the property from the Windows ENV using a "dash". Spring Boot maps this ENV perfectly to the application property.
On Linux, however, it does not accept a "dash" (e.g., "-") in its ENV, so my .profile blows up when I use the same approach that I used on Windows >> APPLICATION_SERVER_JGROUPS-PORT = 5445. What do I need to do to make Linux ENV settings set my "application.server.jgroups-port" property?
EDIT:
It looks like the org.springframework.core.env.SystemEnvironmentPropertySource is where I'd need to do some work to support a dashed property name in Java as a Linux ENV. For instance, a call to getProperty("somePrefix.foo-suffix")
= APPLICATION_SERVER_JGROUPS_PORT in the SystemEnvironmentPropertySource just like it had a period - getProperty("somePrefix.foo.suffix")
You can also provide an environment variable named SPRING_APPLICATION_JSON
with valid JSON in it. This will allow you to override keys with special characters. For example :
export SPRING_APPLICATION_JSON='{"application.server.jgroups-port": 8080}'
This way, you can also override arrays defined in yaml like this :
foo.bar:
- 1
- 2
- 3
with:
export SPRING_APPLICATION_JSON='{"foo.bar": ["4","5","6"]}'
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
Your second attempt should work. According to the Spring Boot Reference Documentation:
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.
I just tested a very similar usecase and it worked well for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With