"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot Internals Date : 20/09/2024 (Session - 29) _____________________________________________________________________________________________________________________________ Today Session ============= Working with Properties File ============================ * By default every spring boot application contains one properties file i.e.,application.properties * This application.properties file will contains configuration related to our Spring Boot Applications 1) Database Configuration 2) Security Configuration 3) Application configuration 4) profile configuration etc., * We no need to provide any sort of configuration related to application.properties file in Spring Boot Application for reading properties file data. * Inorder to read the data from propertie file we need to use the annotation @value in Spring bean class to read the property value from properties file. application.properties ====================== #Specifying the Banner Location spring.banner.location=classpath:banner.txt #Setting some Application Name spring.application.name=FirstSpringBootApplication #Employee Information employeeId=AIT123 employeeName=Mahesh designation=Software Engineer salary=25000 OUTPUT ====== _ _ _ ___ _____ / \ ___| |__ ___ | | __ |_ _|_ _| / _ \ / __| '_ \ / _ \| |/ / | | | | / ___ \\__ \ | | | (_) | < | | | | /_/ \_\___/_| |_|\___/|_|\_\ |___| |_| 2024-09-20T06:54:48.579+05:30  INFO 3028 --- [08_SpringBoot_FirstApp] [ main] com.ashokit.Application  : Starting Application using Java 17.0.7 with PID 3028 (G:\ASHOKIT_WORKSPACE\47_SPRINGBOOT_AND_MICROSERVICES\APP_Workspace\08_SpringBoot_FirstApp\target\classes started by Ganesh in G:\ASHOKIT_WORKSPACE\47_SPRINGBOOT_AND_MICROSERVICES\APP_Workspace\08_SpringBoot_FirstApp) 2024-09-20T06:54:48.582+05:30  INFO 3028 --- [08_SpringBoot_FirstApp] [ main] com.ashokit.Application  : No active profile set, falling back to 1 default profile: "default" WishController Class Constructor WishService Class Constructor..... WishDao Class Constructor.... Demo Runner Email Runner Test Runner 2024-09-20T06:54:49.327+05:30  INFO 3028 --- [08_SpringBoot_FirstApp] [ main] com.ashokit.Application  : Started Application in 1.158 seconds (process running for 1.815) Email Runner Executed :::::[--spring.output.ansi.enabled=always, mahesh.ashokit@gmail.com, --username=manager, ashokitschools@gmail.com, --password=ashokit] Test Runner Executed :::::[--spring.output.ansi.enabled=always, mahesh.ashokit@gmail.com, --username=manager, ashokitschools@gmail.com, --password=ashokit] CommandLineRunner With lambda Expressions-2....... Demo Runner Executed :::::[--spring.output.ansi.enabled=always, mahesh.ashokit@gmail.com, --username=manager, ashokitschools@gmail.com, --password=ashokit] Non-Option Values ::::[mahesh.ashokit@gmail.com, ashokitschools@gmail.com] spring.output.ansi.enabled======always password======ashokit username======manager CommandLineRunner With lambda Expressions....... Welcome To AshokIT For SpringBoot Development com.ashokit.controller.WishController@44de94c3 Fri Sep 20 06:54:49 IST 2024 Employee [empId=AIT123, empName=Mahesh, jobDesignation=Software Engineer, empSalary=25000] Important Points on @Value Annotation ************************************** 1) @Value Annotation is provided by Spring Framework 2) @Value Annotation can be used in both Spring Application & Spring Boot Application. 3) @Value Annotation is Field/Property level Annotation. 4) @Value Annotation can perform the below things - We can define the static values to Spring bean Properties - We can Assign the values from Properties file to spring bean properties - We can assign the system property values to Spring bean properties - We can assign the environmental variable values to Spring bean properties 5) When we are working with this annotation there is no mandatory to match the keys in properties file and spring bean Property names/Field Names. 6) Spring Container internally uses Reflection API for injecting value from property file into spring bean properties. 7) @Value Annotation performs single value Injection not supports for Bulk values Injection. Example Application with Properties file ======================================== Employee.java ============= 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.properties ====================== #userdefined keys com.ashokit.id=123 com.ashokit.name=Mahesh com.ashokit.salary=50000 #Object property keys com.ashokit.address.doorNo=1-2-3 com.ashokit.address.streetName=Ameerpet com.ashokit.address.cityName=Hyderabad #supplying the Array of Values i.e,Index based Injection #com.ashokit.colors[0]=Yellow #com.ashokit.colors[1]=Red #com.ashokit.colors[2]=Green #com.ashokit.colors[3]=Blue #supplying the array of valuesi.e.,Inline value based injection com.ashokit.colors=black,Yellow,Green,Blue,Orange OUTPUT ====== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ Employee [id=123, name=Mahesh, salary=50000.0, address=Address [doorNo=1-2-3, streetName=Ameerpet, cityName=Hyderabad], colors=[black, Yellow, Green, Blue, Orange]] Important Points on @ConfigurationProperties annotation ======================================================== 1) This annotation supports for "Bulk Injection" based on common prefix and its Spring bean properties. 2) This annotation is provided by Spring boot and we can't use this annotation in spring application. 3) While working with these annotation make sure need to generate the setter methods for Spring bean properties. 4) While working with these annotation make sure need to add the below dependency in pom.xml org.springframework.boot spring-boot-configuration-processor true 5) Make sure use this annotation on top of Spring bean class. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IIQ) Can we have our own properties file in spring boot application apart from application.properties file? Ans) - Yes, We can have our own properties file in spring boot application. - But Programmer need to configure the user defined property file using @PropertySource Annotation. - The above annotation can be declared on top Main Class in Spring Boot Application +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IIQ) If we have same keys in predefined properties file(application.properties) and user defined property file (employee.properties) which values will be considered? Ans) The values in application.properties file will override the values given sample.properties file.So finally application.properties files will be injected as the final value. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++