Spring Boot: Using YAML file inspite of Property files

Subscribe to my newsletter and never miss my upcoming articles

Using YAML file

Yaml as they are called Yet Another Markup Language is an alternative to properties file. It has extension of “.yml”. We can create application.yml file instead of application.properties.

Following it table for equivalent properties and YAML

application.properties

app.name=User Application
app.version: 1.0.0
app.description = ${app.name} - ${app.version} is an application to store user details.>
app.developers.name[0]=Ray McFallen
app.developers.name[1]=John Smith

application.yml

app:
  name : User Application
  Version: 1.0.0
  description: ${app.name} - ${app.version} is an application to store user details.
  developers:
    name:
      - Ray McFallen
      - John Smith

As we can see yaml file is less repetitive than properties file in term of key namings. The another difference to be noticed is in list of elements (Array in above case) .

Yaml file is lot cleaner approach. With properties file we are creating profile specific properties file i.e. application-{profile}.properties . Which means for each profile there will be separate properties file. With yml , we can use three “-” like — and write profile specific values in same file.

app:

  name : User Application

  Version: 1.0.0

---

spring:

  profiles: development

  Version: 1.0.40

---

spring:

  profiles: production

  Version: 1.0.3

Here version will change if application is started with profiles as development and production but by default (if no profile is given) it will pick 1.0.0 .

The biggest shortcoming of YAML is that you can’t use it with @PropertySource annotation. This annotation is used to load property files other named other than application*properties . PropertySource annotation is used for externalizing properties.

No Comments Yet