SpringBoot9AM 18122020
SpringBoot9AM 18122020
YAML ( __.yml )
--application.properties---
my.app.id=10
------------------------
--application.yml--
my:
app:
id: 99
--------------------
@Value("${my.app.id}") ---> 10
https://www.google.com/search?q=spring+boot+2.4.1
https://github.com/spring-projects/spring-boot/releases/tag/v2.4.1
application.yml
application.properties
---------------------------------------------------------------
YAML Programming -- Configuration Properties
--code-------------------
#1 Create Spring Boot Starter Project
Name : SpringBoot2YamlExOne
Package : in.nareshit.raghu
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "my.app")
public class MyDataRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(this);
}
@Override
public String toString() {
return "MyDataRunner [id=" + id + ", code=" + code + ", cost=" + cost +
"]";
}
}
#3. application.yml
my:
app:
id: 88
code: AA
cost: 9.9
========Ex#2===============================================================
YAML with List/Set/Array and Map
import java.util.Arrays;
import java.util.Map;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//ctrl+shift+O
@Component
@ConfigurationProperties(prefix = "my.app")
public class MyCollDataReader implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(this);
}
S#3 application.yml
my:
app:
models:
- AA
- BB
- CC
grades:
A: 1
B: 2
C: 3
---------
Output: MyCollDataReader [models=[AA, BB, CC], grades={A=1, B=2, C=3}]
====================================================================
YAML using HAS-A Relation : using class as a DataType
S#2 Model
package in.nareshit.raghu.model;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import in.nareshit.raghu.model.Student;
//ctrl+shift+O
@Component
@ConfigurationProperties(prefix = "my.app")
public class HasADataRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(sob);
}
S#4 application.yml
my:
app:
sob:
sfee: 77.9
sid: 999
sname: AAA
----------
Output: Student [sid=999, sname=AAA, sfee=77.9]
=====================================================================
Comment in properties/yml --> # symbol