Home > OS >  Override Spring Boot yaml property via environment variable
Override Spring Boot yaml property via environment variable

Time:12-08

Using Spring Boot 2.6.1, If I have an application.properties file that looks like:

spring.datasource.url="jdbc://blahblah"

I can override that value at runtime with an environment variable named spring.datasource.url and my application will connect to the database specified in the env var.

However, if I have an equivalent application.yaml file, specifying the environment variable that way appears to have no effect.

spring:
  datasource:
    url: "jdbc://localhost..."

However, if I rename my environment variable to SPRING_DATASOURCE_URL, the override works again. This appears to be consistent across other properties as well (not just the datasource url).

Looking through the docs it wasn't obvious why this should be the case, except that yaml configuration seems like it's generally treated a little different than "normal" properties files.

Is this behaviour expected?

CodePudding user response:

If i did not misunderstand, you should create application-{profile}.yml files and specify datasource url for environments

CodePudding user response:

As described in the documentation, you should use the environment variable SPRING_DATASOURCE_URL to set the spring.datasource.url property. I am surprised that spring.datasource.url worked at all when configured as an environment variable and I would not rely on it continuing to do so.

  • Related