Modern containerised 12 factor applications are expected to derive their configuration from environment variables. While Spring Boot does import some of its properties from environment variables, it doesn’t for most of its common properties.

This post is part of the “Spring Boot Primer” series.

To avoid littering your code with environment variable names, and allow you to configure the common Spring Boot properties it’s simply a matter of using variable interpolation in your application.yml or application.properties.

application.ymlapplication.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:

  datasource:
    url: jdbc:${DB_VENDOR:mariadb}://${DB_ADDR:localhost}:${DB_PORT:3306}/${DB_NAME:backend}
    username: ${DB_USER:backend}
    password: ${DB_PASSWORD:XfeCEtSOFL91QpeyDxQnkRattHWzufTdDB1Pn5iB4}
    driver-class-name: ${DB_DRIVER:org.mariadb.jdbc.Driver}

  session:
    store-type: ${SESSION_STORE_TYPE:redis}

  redis:
    host: ${SESSION_HOST:localhost}
    password: ${SESSION_PASSWORD:}
    port: ${SESSION_PORT:6379}

To allow you to set sensible defaults, so that your application can be started straight from your IDE with no additional configuration, you can also embed your development environment defaults, with the normal BASH-style syntax:

${DB_PASSWORD:XfeCEtSOFL91QpeyDxQnkRattHWzufTdDB1Pn5iB4}

At development-time (either running the app locally in your IDE / CLI or in Docker), you don’t need to provide any additional configuration; making set-up for new developers on your team very rapid.

At production-time, all of these properties can easily be set via your docker-compose.yml:

docker-compose-production.ymldocker-compose-production.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
services:
  backend:
    image: surevine/spring-rest-example:latest
    environment:
      DB_VENDOR: 'mariadb'
      DB_ADDR: 'db'
      DB_NAME: 'backend'
      DB_USER: 'backend'
      DB_PASSWORD: 'WVn1X9JAZixu7bOCfITFSQyfru4wtRdqztf9PHE3s'
      DB_DRIVER: 'org.mariadb.jdbc.Driver'
      SESSION_HOST: 'cache'
      SESSION_PASSWORD:
      SESSION_PORT: 6379
      MEDIA_LOCATION: 'file:/var/lib/data/'
    ports:
      - '8080:8080'

or (even better) pull out into a secure key store such as LastPass.