Issue
I have an application.properties
file that looks like this:
mcl.sso.frontend-url=http://blah.com:9001
mcl.sso.mocking-agent=false
I am trying to override these two variables from the command line. This should be possible by setting environment variables. Here's how I'm running the command:
MCL_SSO_FRONTEND_URL='foobar' MCL_SSO_MOCKING_AGENT='true' ./gradlew run
However, when I print out the values of these variables, mcl.sso.mocking-agent
equals "true" (as expected), but mcl.sso.frontend-url
still equals "http://blah.com:9001" (unexpected). Why doesn't mcl.sso.frontend-url
change the value of the property? I can only assume this has something to do with the way Spring converts environment variables into property keys, but I can't find any specific documentation on this.
Solution
From spring-boot documentation:
Binding from Environment Variables
Most operating systems impose strict rules around the names that can be used for environment variables. For example, Linux shell variables can contain only letters (a
to z
or A
to Z
), numbers (0
to 9
) or the underscore character (_
). By convention, Unix shell variables will also have their names in UPPERCASE.
Spring Boot’s relaxed binding rules are, as much as possible, designed to be compatible with these naming restrictions.
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
.
Answered By - MrSpock
Answer Checked By - Mildred Charles (JavaFixing Admin)