According to the documentation it is possible to override the spring properties via environment variables following these roles:
- Replace dots (.) with underscores (_).
- Remove any dashes (-).
- Convert to uppercase.
This project has the following yaml file:
spring:
datasource:
data-input:
jdbc:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driverClassName: wrongValue
username: sa
password: password
To override the driverClassName
following the above rule, we should set the environment variable
SPRING_DATASOURCE_DATAINPUT_JDBC_DRIVERCLASSNAME
However, this is not the case. As show in the test
the Environment
won't return the overridden value by SPRING_DATASOURCE_DATAINPUT_JDBC_DRIVERCLASSNAME
. Interestingly, the
driver is correctly set in the DataSource
where property are bonded via the @ConfigurationProperties
annotation.
The opposite scenario happens when is the SPRING_DATASOURCE_DATA_INPUT_JDBC_DRIVERCLASSNAME
to being set. In this case the
DataSource
initialization will fail because the driver value is incorrect (wrongValue
), while the Environment
will return
the value set via the environment variable.