application.properties vs application.yml in Spring Boot application.properties vs application.yml in Spring Boot

Page content

In this tutorial, we’ll compare Spring Boot Configuration using YAML file and Property file and their advantages one over the other.

Overview

A common practice in Spring Boot project is to externalize the configuration through a properties file, YAML file, environment variable, or command-line arguments.

Properties file application.properties is widely used for external configuration, however YAML file application.yml is gaining popularity due to its simplicity and JSON like format.

Hierarchical Configuration

application.properties

Each line is a single configuration in .properties file. Therefore, we need to use some prefixes to define hierarchical configuration such as below example, where spring.datasource is a prefix for url, username, and password configuration.

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=SA
spring.datasource.password=password
application.yml

On the other hand .yml files are very convenient to define hierarchical data which is very similar to JSON format

spring:
   datasource:    
      url: jdbc:mysql://localhost/test
      username: SA
      password: password

Clearly YAML file is more readable as compare to Properties file.

Note that you use consistent spaces in next line to represent nested data in YAML file.

List Configuration

application.properties

If you have a list of configurations then they can be defined in .properties file -

  • Individually in different line using indices such as servers[0], server[1] … ,or
  • Together in single line using comma separated values such as dev, prod
application.servers[0]=localhost
application.servers[1]=abc.test.com
application.servers[2]=xyz.dev.com
application.environment=dev, prod
application.yml

Similar list of configuration can be defined in .yml file -

  • Individually in different line prefixed with - , or
  • Together in single line using comma separated values such as [dev, prod]
application:
   servers:    
      - localhost
      - abc.test.com
      - xyz.dev.com
   environment: [dev, prod]

Nested List Configuration

YAML file becomes more concise and readable as compare to Properties file when the List configuration contains further nested data.

application.properties
application.servers[0].ip=127.0.0.1
application.servers[0].path=/path1
application.servers[1].ip=127.0.0.2
application.servers[1].path=/path2
application.servers[2].ip=127.0.0.3
application.servers[2].path=/path3
application.yml
application:
   servers:
      - ip: '127.0.0.1'
        path: '/path1'
      - ip: '127.0.0.2'
        path: '/path2'
      - ip: '127.0.0.3'
        path: '/path3'

Map Configuration

application.properties

A Map configurations can be defined in .properties file -

  • Individually in different line using key=value pair … ,or
  • Together in single line using comma separated values such as { key1: "value1", key2: "value2"}
map1.key1=value1
map1.key2=value2
map2={ key1: "value1", key2: "value2" }
application.yml

Similar Map configuration can be defined in .yml file -

  • Individually in different line using key: value pair, or
  • Together in single line using comma separated values such as { key1: "value1", key2: "value2"}
map1:
   key1: value1
   key2: value2
map2: { key1: "value1", key2: "value2" }

Profile Based Configuration

A common practice in Spring Boot configuration is to have YAML or Properties file for each profile (environment) - test, dev, staging, prod.

Spring Boot picks up .properties or .yml files in the following sequence:-

  1. application-{profile}.properties | .yml
  2. application.properties | .yml

We can pass the profile (for e.g. prod) from command-line argument for e.g. --spring.profiles.active=prod or from application.properties | .yml

application.properties

In case of multiple profiles, each profile is configured in different application-{profile}.properties file.

application.properties

#Logging
logging.level.root=ERROR
logging.level.org.springframework.web=INFO
logging.level.com.codingnconcepts=DEBUG

#spring
spring.main.banner-mode=off
spring.profiles.active=dev

server.email=default@codingnconcepts.com

application-dev.properties

#dev environment
server.email: dev@codingnconcepts.com
server.cluster[0].ip=127.0.0.1
server.cluster[0].path=/dev1
server.cluster[1].ip=127.0.0.2
server.cluster[1].path=/dev2
server.cluster[2].ip=127.0.0.3
server.cluster[2].path=/dev3

application-prod.properties

#production environment
server.email: prod@codingnconcepts.com
server.cluster[0].ip=192.168.0.1
server.cluster[0].path=/app1
server.cluster[1].ip=192.168.0.2
server.cluster[1].path=/app2
server.cluster[2].ip=192.168.0.3
server.cluster[2].path=/app3

Properties which are not configured in application-{profile}.properties such as logging.level, are picked up from application.properties file.

Let’s pass different profiles using command-line arguments:-

# The 'dev' profile is configured in application.properties

# Profile: dev, picks application-dev.properties or YAML
$ java -jar target/spring-boot-profile-1.0.jar

ServerProperties{email='dev@codingnconcepts.com', cluster=[
	Cluster{ip='127.0.0.1', path='/dev1'},
	Cluster{ip='127.0.0.2', path='/dev2'},
	Cluster{ip='127.0.0.3', path='/dev3'}
]}

# Profile: prod, picks application-prod.properties or YAML
$ java -jar -Dspring.profiles.active=prod target/spring-boot-profile-1.0.jar

ServerProperties{email='prod@codingnconcepts.com', cluster=[
	Cluster{ip='192.168.0.1', path='/app1'},
	Cluster{ip='192.168.0.2', path='/app2'},
	Cluster{ip='192.168.0.3', path='/app3'}
]}

# Profile: abc, a non-existent profile
$ java -jar -Dspring.profiles.active=abc target/spring-boot-profile-1.0.jar
ServerProperties{email='null', cluster=[]}

application.yml

We can create multiple profiles in single .yml file by using three dashes ---.

logging:
  level:
    root: ERROR
    org.springframework.web: INFO
    com.codingnconcepts: DEBUG
spring:
  profiles:
    active: "dev"
  main:
    banner-mode: "off"
server:
  email: default@codingnconcepts.com

---

spring:
  profiles: dev
server:
  email: dev@codingnconcepts.com
  cluster:
    - ip: 127.0.0.1
      path: /dev1
    - ip: 127.0.0.2
      path: /dev2
    - ip: 127.0.0.3
      path: /dev3

---

spring:
  profiles: prod
server:
  email: prod@codingnconcepts.com
  cluster:
    - ip: 192.168.0.1
      path: /app1
    - ip: 192.168.0.2
      path: /app2
    - ip: 192.168.0.3
      path: /app3

Rules for picking up profile specific configuration from YAML file is same as Property file.

Conclusion

In this article, we’ve seen some differences between Properties and YAML Spring Boot configuration files. We saw that YAML is more human friendly and concise compare to Properties file.

Also learn how to read configuration properties from YAML file in Spring Boot