Fabio
Fabio

Reputation: 617

spring boot YAML default and environment variable override like HOCON files

Is there a way in spring-boot YAML file to do the same as in HOCON files where you can have a default and be able to override it with an environment variable like this:

basedir = "/whatever/whatever"
basedir = ${?FORCED_BASEDIR}

In this case in HOCON if you don't define a environment variable named FORCED_BASEDIR then basedir will be "/whatever/whatever" but if you do then the value of basedir will be whatever is defined in the environment variable.

Thanks

So based on webdizz answer below I looked up a little bit and I found a pretty good description in book "Spring Boot in Action". Here is the hierarchy:

There are, in fact, several ways to set properties for a Spring Boot application. Spring Boot will draw properties from several property sources, including the following:

  1. Command-line arguments
  2. JNDI attributes from java:comp/env
  3. JVM system properties
  4. Operating system environment variables
  5. Randomly generated values for properties prefixed with random.* (referenced when setting other properties, such as `${random.long})
  6. An application.properties or application.yml file outside of the application Licensed to Thomas Snead 58 CHAPTER 3 Customizing configuration
  7. An application.properties or application.yml file packaged inside of the application
  8. Property sources specified by @PropertySource
  9. Default properties

Upvotes: 1

Views: 5087

Answers (1)

webdizz
webdizz

Reputation: 846

Spring Boot provides means to define variables at many levels and your case is supported, you just need to define variable in following way:

in application.yml:

basedir: "/whatever/whatever"

and in environment:

export BASEDIR = "/another/whatever"

Then in runtime application will use value from environment.

For more details check this out enter link description here.

Upvotes: 2

Related Questions