Spring @ConfigurationProperties using YAML Spring @ConfigurationProperties using YAML

Page content

In this article, we’ll learn how to use @ConfigurationProperties with .yml file instead of .properties file for configuration in Spring Boot Project.

application.yml

YAML (YAML Ain’t Markup Language) which is defined as .yml file, is becoming popular for configuration over traditional .properties file because of its simplicity and readability.

# Peson properties
person:
  # string
  name: Ashish Kumar Lahoti
  # string
  occupation: programmer
  # int
  age: 33
  # float
  gpa: 3.5
  # double
  fav_num: 1e+10
  # boolean
  male: true
  # date
  birthday: 1986-08-22
  # empty
  flaws: null
  # array
  hobbies:
    - bike riding
    - watching movies
    - online games
    - cooking
  # array
  movies: ["Dark Knight", "Spider Man", "Thor"]
  # map
  assets: {Car: 1, Bike: 2, Home: 1}
  # complex map
  size:
    t-shirt:
      us: XL
      uk: L
    shoes:
      us: 8.5
      uk: 6.5 
  # object array
  friends:
    - name: "eve"
      age: 28
    - {name: "adam", age: 26}
    -
      name: "chloe"
      age: 40
  # multi line string using >
  description: >
    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit, sed
    do eiusmod tempor incididunt ut
    labore et dolore magna aliqua    
  # multi line string with line break preserved using |
  signature: |
    Thanks & Regards,
    Ashish Kumar Lahoti
    email - lahoti.ashish20@gmail.com    

In the application.yml, we can define:-

  1. Primitive types - String, int, float, double, boolean
  2. Date in standard date formats like YYYY-MM-DD
  3. Properties with empty or null values
  4. Array in two ways:-
    • Add new line for each element prefixed by -
    • Add comma separated values like [ item1, item2, …]
  5. Map - key value pairs like { Key1: value1, Key2: value2, …. }
  6. Array of Objects
  7. Multiline String using prefix > or |. Please note that line break is preserved when you use |

YAML Syntax Tips

In YAML:

  • Spacing matters
  • Line Starts with hash (#) are comments and ignored.

In general, you do not need to use quotation "" around string values in YAML. Quotation is requires if it:

  • includes a colon (:), hash (#), greater than (>), or any character that has a special meaning in YAML,
  • looks like a number (decimal, hexadecimal, exponential E or e notation, etc.),
  • or is one of YAML’s reserved boolean words (true, false, no, off, etc.).

@ConfigurationProperties

We have defined person properties in our application.yml. Let’s map these properties to a configuration class file using @ConfigurationProperties annotation at class level

@Configuration
@ConfigurationProperties("person")
public class PersonConfig {

	private String name;
	private String occupation;
	private int age;
	private float gpa;
	private double favNum;
	private boolean male;
	private String birthday;
	private String flaws;
	private String[] hobbies;
	private List<String> movies;
	private Map<String, Integer> assets;
  private Map<String, Map<String, String>> size;
	private List<Friend> friends;
	private String description;
	private String signature;	  
    // ... getters and setters
}

class Friend {

	private String name;
	private int age;
    // ...getters and setters
}

We have annotated PersonConfig class with @ConfigurationProperties("person"). Spring boot will intialize the fields of PersonConfig with values from .yml file prefixed with “person” property for e.g. PersonConfig’s name field value will be initialized from “person.name” property.

Relaxed Binding

Spring Boot @ConfigurationProperties annotation supports relaxed binding which means that

person.fav-num
person.favNum
person.fav_num
person.FAV_NUM

Any of the above .yml property format can be mapped to PersonConfig’s favNum field.

Summary

In this article, we learned that Spring Boot configuration using .yml file is much simpler as compared to .properties file. If we compare .yml file of above example with .properties file. It will look something like this:-

application.properties
person.name = Ashish Kumar Lahoti
person.occupation = programmer
person.age = 33
person.gpa = 3.5
person.fav_num = 1e+10
person.male = true
person.birthday = 1986-08-22
person.flaws = null
person.hobbies[0] = bike riding
person.hobbies[1] = watching movies
person.hobbies[2] = online games
person.hobbies[3] = cooking
person.movies = ["Dark Knight", "Spider Man", "Thor"]
person.assets = {'Car': 1, 'Bike': 2, 'Home': 1}
person.friends[0].name = "adam"
person.friends[0].age = 28
person.friends[1].name = "ben"
person.friends[1].age = 26
person.friends[1].name = "chloe"
person.friends[1].age = 40
person.description = Lorem ipsum dolor sit amet,\n consectetur adipiscing elit, sed\n do eiusmod tempor incididunt ut \n labore et dolore magna aliqua
person.signature = Thanks & Regards, \n Ashish Kumar Lahoti \n email - lahoti.ashish20@gmail.com