Spring Boot Application Properties and YAML

Published
Updated

Spring Boot Application Properties and YML

In a typical Maven project setup, the application.properites or application.yml files live within the /src/main/resources folder of your application’s project structure.

spring-boot-application-properties-yaml

Application Properties File

some.property.here=Spring Properties File

Application YAML File

some:
  property:
    here: Spring YAML File

You can then inject properties into your Spring Boot application’s java code by using the @Value annotation:

import org.springframework.beans.factory.annotation.*;

@Component
Public class MyAppBean {

    @Value("${some.property.here}")
    private String myDynamicVariable;
    
}

Depending on which application properties or YML file that is loaded, the value of myDynamicVariable will differ. This is incredibly useful for variables that are environment dependent or ones that can change based on the active Spring Profile.

Application Properties and YAML Profiles

Depending on the Maven or Spring Boot profile you are using, you can have different application.properties or application.yml files which represent configurations for the respective environment.

spring-boot-profiles-yaml

For example:

  • Use a application-dev.properties file which would be used if your application is running in a development environment.
  • Use a production specific application-prod.properties which is used when running your application within production.

The appended dev or prod keys match the Spring profile which is currently active.

Combining, Overriding, and Inheritance in Properties

What happens when you have properties or definitions that remain the same across different profiles?

Conveniently, Spring has built in a configuration inheritance in which values can be defined in one file and superseded or override one or more times in further property files.

For example, say I define a property my.favorite.color=blue within my application.properties file. If no Spring profile is configured, the value will remain set to blue. Should one define a dev profile, you could have your application-dev.properties overwrite this and set it to red.

Spring Boot application.properties inheritance diagram example

You can define multiple Spring profiles by chaining them in the order you want them to override one another:

mvn spring-boot:run -Dspring.profiles.active=dev,prod

In this situation, any values defined in dev that are also defined within the prod file would be overwritten, because prod is defined after dev.

You can mix application.properties files with application.yml files too! These YAML files follow the aforementioned pattern of inheritance and profiling. You can have an application.yml and an application-dev.yml file that overrides specific values for a dev centric environment.

YAML vs Properties files

By default, Spring Boot applications support YAML as an alternative to properties files if you are using any of the spring-boot-starter dependencies. If you wish to use YAML documents to inject configuration data into your Spring Boot application, you will need to ensure that they align correctly to match the hierarchy of a property file. For example:

server:
  port: 8080
  servlet:
    context-path: /
    
logging:
  level:
    root: DEBUG
    org.springframework.web: DEBUG
    
myapp:
  domain-list:
  - something.com
  - example.net

The above would translate to the following properties file:

server.port=8080
server.servlet.context-path=/

logging.level.root=DEBUG
logging.level.org.springframework.web=DEBUG

myapp.domain-list[0]=something.com
myapp.domain-list[1]=example.net

External application properties and yml files

Should you have properties or yaml files external to your project file or directory, you can include them by adding the following parameter when invoking your Spring Boot application:

mvn spring-boot:run -Dspring.config.location="file://path/to/application.properties"

Alternatively you can specify a path to your YAML or properties in an environment variable like so:

# Loads application.properties and application.yml files
# (including files appended with your Spring environment such as
# application-dev.properties or application-prod.yml)

export SPRING_CONFIG_NAME=application
export SPRING_CONFIG_LOCATION=file://path/to/config/folder/
java -jar SpringBootApplication.jar

Note that the environment variables are using underscores instead of periods to separate the key names. Knowing this pattern, you can include other properties in this same manner.

Finally, you can load properties files via a properties file (or YAML for that matter), by adding a line like so in your application.properties: (this feature is new to Spring Boot 2.4)

spring.config.import=file:./supplementary.yml,optional:file:./anotherfile.properties

You can also include entire folders via the filesystem or classpath or using wildcards :

spring.config.import=optional:classpath:/config/*/
spring.config.import=optional:file:/config/*/

Notice the optional: prefix? That tells Spring not to worry if the referenced properties file cannot be found, and to keep on bootstrapping.