"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : YAML File Date : 01/04/2025 (Session - 17) _____________________________________________________________________________________________________________________________ Yesterday Session ================== * We discussed about properties files and reading data from properties file * One to One Property mapping -> @Value Annotation -> Spring & Spring Boot Application * One to Many Property mapping -> @Configurationproperties Annotations -> Spring Boot Today Session ============= Working with YAML File In Spring Boot Application ************************************************* * YAML Stands for "Yet Another Markup Language". * Spring Boot Supports another type of configuration file i.e., YAML File and its extension is .yml (or).yaml file. * The Biggest limitation of properties file is "common prefix" is repeated for every key in properties file, In order to avoid such kind of repeation need to go for "YAML Files" concept. * We can read the YAML File data by using @Value & @ConfigurationProperties Annotations. * Spring Boot internally converts every yml file content into properties file content and to convert this by spring boot using SnakeYml API with following jar file(snakeyml-version.jar) from spring boot classpath. * Below example for writing the yaml file based on properties file *********************** application.properties *********************** com.ashokit.employeeId=123 com.ashokit.employeeName=Mahesh ` com.ashokit.designation=software Engineer com.ashokit.salary=50000 *********************************** application.yml(or)application.yaml *********************************** com: ashokit: employeeId: 1001 employeeName: Mahesh designation: Software Engineer salary: 50000 IIQ) Differences between Properties File (Vs) YAML File *************** Properties File *************** 1) There is no specification provided rules and guidelines for developing Properties file. 2) In properties file data will be stored in the form of key-value pairs. 3) Properties file can be used only for Java Application. 4) It can be supported both Spring Framework and Spring boot framework. 5) While working with profiles in spring/spring boot we can't place multiple profiles information into single property file. 6) Nodes/Levels in the keys may have duplicates. 7) Spring or Spring Boot application directly loads and reads the properties file content. ********** YAML File ********** 1) There is specification provided rules and guidelines for developing yml file(www.yml.org) 2) In YAML file data will be stored in the form of tree structure. 3) YAML files can be used following programming languages i.e.,Java,Python,Ruby,Groovy etc., 4) It can't be used in Spring Framework and it only supported Spring Boot Application. 5) We can place multiple profiles in single yml file having seperation with "---". 6) Same Nodes/Levels will not repeated (no duplicates). 7) Every YAML file will be converted to properties file before loading and reading. Reading data from YAML File To Spring Bean classes ================================================== package com.ashokit.beans; import java.util.Arrays; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "com.ashokit") public class Employee { private String id; private String name; private float salary; private Address address; private String[] colors; public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSalary(float salary) { this.salary = salary; } public void setAddress(Address address) { this.address = address; } public void setColors(String[] colors) { this.colors = colors; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", address=" + address + ", colors=" + Arrays.toString(colors) + "]"; } } Address.java ============ package com.ashokit.beans; import org.springframework.stereotype.Component; @Component public class Address { private String doorNo; private String streetName; private String cityName; public void setDoorNo(String doorNo) { this.doorNo = doorNo; } public void setStreetName(String streetName) { this.streetName = streetName; } public void setCityName(String cityName) { this.cityName = cityName; } @Override public String toString() { return "Address [doorNo=" + doorNo + ", streetName=" + streetName + ", cityName=" + cityName + "]"; } } application.yml(Version-1) ========================== com: ashokit: id: AIT123 name: mahesh salary: 25000 address: doorNo: 12-13-14 streetName: XYZ cityName: Hyderabad colors: red,black,brown application.yml(Version-2) ========================== com: ashokit: id: AIT123 name: mahesh salary: 25000 address: doorNo: 12-13-14 streetName: XYZ cityName: Hyderabad colors: - Red - Black - Blue - Brown NOTE ==== * We can convert the existing properties file into yaml file in STS IDE by simply right clicking on properties file and choose the option "convert into yaml file". * We can take the support of online converter from properties <--------------------> YAML File Application.java ================ package com.ashokit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.PropertySource; import com.ashokit.beans.Employee; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); Employee emp = context.getBean(Employee.class); System.out.println(emp); } } OUTPUT ====== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ Employee [id=AIT123, name=mahesh, salary=25000.0, address=Address [doorNo=12-13-14, streetName=XYZ, cityName=Hyderabad], colors=[red, black, brown]] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IIQ) Can we have both application.properties and application.yml file in spring boot application? Ans) Yes, We can have both files in spring boot application. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IIQ) If we place both application.properties and application.yml file having same keys and different values? Ans) Finally values from application.properties file only ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++