Override Spring Boot yaml property via environment variable
Asked Answered
S

1

6

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?

Surety answered 7/12, 2021 at 22:35 Comment(0)
S
13

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.

Schoen answered 7/12, 2021 at 22:47 Comment(1)
I was too, to be honest, but saw it in another project and the thinking was "hey, now the env vars match the property names exactly!" Oh well... upper-case it is :)Surety

© 2022 - 2024 — McMap. All rights reserved.