Yaml files in Spring Boot

Yaml: Yet Another Markup Language[Old]
Yaml: Yaml Ain’t Markup Language[New]

Yaml is a text represented file to represent data and to transport data over the network, it is the same as XML, JSON,…files.

In general, Yaml files are created by using the “.yml” extension.

Inside the Yaml files, we are able to provide the data in the form of key-value pairs with indentation operators or levels.

In Spring Boot applications, we are able to use Yaml files as an alternative to the properties files.

Writing YAML files

In Spring Boot applications, we are able to provide yaml files under the resources folder with the default name “application.yml”.

EX:

application.properties
server.port=1234
application.yml
server:
port: 1234

Note: In Spring Boot applications, all conventions of the properties files are applicable to the yaml files.

application.yml
server:
port: 1234
spring:
application:
name: app12

employee:
eno: 111
ename: Durga
esal: 50000
eaddr: Hyd

student:
sid: S-111
sname: Anil
saddr: Hyd

AppRunner.java — class implementing CommandLIneRunner.

package com.durgasoft.app12.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private Environment env;

@Override
public void run(String... args) throws Exception {
System.out.println("Application Details");
System.out.println("------------------------");
System.out.println("Server Port Number : "+env.getProperty("server.port"));
System.out.println("Application Name : "+env.getProperty("spring.application.name"));
System.out.println();

System.out.println("Employee Details");
System.out.println("-------------------------");
System.out.println("Employee Number : "+env.getProperty("employee.eno"));
System.out.println("Employee Name : "+env.getProperty("employee.ename"));
System.out.println("Employee Salary : "+env.getProperty("employee.esal"));
System.out.println("Employee Address : "+env.getProperty("employee.eaddr"));
System.out.println();

System.out.println("Student Details");
System.out.println("----------------------------");
System.out.println("Student Id : "+env.getProperty("student.sid"));
System.out.println("Student Name : "+env.getProperty("student.sname"));
System.out.println("Student Address : "+env.getProperty("student.saddr"));
}
}

App12Application.java — Spring Boot Application class

package com.durgasoft.app12;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App12Application {

public static void main(String[] args) {
SpringApplication.run(App12Application.class, args);
}
}

Output :

Application Details
------------------------
Server Port Number : 1234
Application Name : app12

Employee Details
-------------------------
Employee Number : 111
Employee Name : Durga
Employee Salary : 50000
Employee Address : Hyd

Student Details
----------------------------
Student Id : S-111
Student Name : Anil
Student Address : Hyd

Importing YAML files

In a Single Spring Boot Application, we are able to provide more than one Yaml file, but all the yaml files must be imported into the application.yml file by using the following code.

spring:
config:
import:
- student.yml
- customer.yml
- employee.yml

Example :

student.yml
student:
sid: S-111
sname: AAA
saddr: Hyd

employee.yml

employee:
eno: 111
ename: BBB
eaddr: Chennai

customer.yml
customer:
cid: C-111
cname: CCC
caddr: Pune

application.yml
spring:
application:
name: app13
config:
import:
- student.yml
- employee.yml
- customer.yml

AppRunner.java

AppRunner.java
package com.durgasoft.app13.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/*@PropertySources(
{
@PropertySource("classpath:student.yml"),
@PropertySource("classpath:employee.yml"),
@PropertySource("classpath:customer.yml")
}
)*/
/*

@Configuration
@ImportResource(
{
"classpath:student.yml",
"classpath:employee.yml",
"classpath:customer.yml"
}
)
*/

@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private Environment environment;

@Override
public void run(String... args) throws Exception {

System.out.println("Student Details");
System.out.println("----------------------");
System.out.println("Student Id : "+environment.getProperty("student.sid"));
System.out.println("Student Name : "+environment.getProperty("student.sname"));
System.out.println("Student Address : "+environment.getProperty("student.saddr"));
System.out.println();

System.out.println("Employee Details");
System.out.println("-------------------------");
System.out.println("Employee Number : "+environment.getProperty("employee.eno"));
System.out.println("Employee Name : "+environment.getProperty("employee.ename"));
System.out.println("Employee Address : "+environment.getProperty("employee.eaddr"));
System.out.println();

System.out.println("Customer Details");
System.out.println("-------------------------");
System.out.println("Customer Id : "+environment.getProperty("customer.cid"));
System.out.println("Customer Name : "+environment.getProperty("customer.cname"));
System.out.println("Customer Address : "+environment.getProperty("customer.caddr"));
}
}

App13Application.java

package com.durgasoft.app13;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App13Application {
public static void main(String[] args) {
SpringApplication.run(App13Application.class, args);
}
}

Output :

Student Details
----------------------
Student Id : S-111
Student Name : AAA
Student Address : Hyd

Employee Details
-------------------------
Employee Number : 111
Employee Name : BBB
Employee Address : Chennai

Customer Details
-------------------------
Customer Id : C-111
Customer Name : CCC
Customer Address : Pune

Q) In Spring Boot applications, if we provide the same property in both application.properties and application.yml files then which value would be taken by Spring Boot Application?

In Spring Boot applications, if we provide the same property in both application.properties and application.yml files then the Spring Boot application will search for the message first in the application.properties file, if the application.properties file is not existed then the Spring Boot application will search for the message in the application.yml file.

Example :

application.properties
message = Hello User, This is from application.properties

application.yml
message:
Hello User, this is from application.yml

AppRunner.java
package com.durgasoft.app13.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private Environment environment;

@Override
public void run(String... args) throws Exception {
System.out.println(environment.getProperty("message"));
}
}

App13Application.java
package com.durgasoft.app13;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App13Application {
public static void main(String[] args) {
SpringApplication.run(App13Application.class, args);
}
}

Output :

Hello User, This is from application.properties

Multiple values to a key

In Spring boot applications, it is possible to provide more than one value for a single key with the ‘,’ separator.

uqual = BTech, MTech, PHD

If we provide more than one value for a single key in the properties file then we are able to get all the multiple values in the form of String[] in the java classes. 

application.properties
spring.application.name=app15
user.name = Durga
user.address = Hyderabad
user.qualifications = BTech, MTech, PHD
user.technologies = JAVA, PYTHON, AWS

User.java - Component or POJO class
package com.durgasoft.app15.beans;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;

@ConfigurationProperties(prefix = "user")
@Component
public class User {

private String name;
private String address;
private String[] qualifications;
private List<String> technologies;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String[] getQualifications() {
return qualifications;
}

public void setQualifications(String[] qualifications) {
this.qualifications = qualifications;
}

public void setTechnologies(List<String> technologies) {
this.technologies = technologies;
}

public List<String> getTechnologies() {
return technologies;
}
}

AppRunner.java
package com.durgasoft.app15.runners;

import com.durgasoft.app15.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private User user;

@Override
public void run(String... args) throws Exception {
System.out.println("User Details");
System.out.println("-------------------");
System.out.println("Name : " + user.getName());
System.out.println("Address : " + user.getAddress());
System.out.print("Qualifications : ");
for (String qualification : user.getQualifications()) {
System.out.print(qualification+" ");
}
System.out.println();
System.out.println("Technologies : "+ user.getTechnologies());
}
}

App15Application.java
package com.durgasoft.app15;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App15Application {

public static void main(String[] args) {
SpringApplication.run(App15Application.class, args);
}
}

In Spring boot applications, inside the yaml files we are able to provide multiple values for a single key by using – symbol.

application.yml
user:
name: Durga
address: Hyderabad
qualifications:
- BTech
- MTech
- PHD
technologies:
- JAVA
- PYTHON
- AWS