0% found this document useful (0 votes)
63 views5 pages

SpringBoot8PM 04072020

vcvcvc

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views5 pages

SpringBoot8PM 04072020

vcvcvc

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Date: 04/07/2020

Spring Boot 7:50 PM


Mr. RAGHU
------------------------------------------------------------------------
Email: [email protected]
FB: https://www.facebook.com/groups/thejavatemple/

Bulk Loading of Properties into code: @ConfigurationProperties

=> if we use @Value, we can read only one value at a time into our class.
=> This time we are using Spring boot concept that loads multiple keys at a time
into our code.

Note:
a. Prefix given in properties file and @ConfigurationProperties("") must be
matching, else values are not taken into application.
Variables holds default values.

b. Must generate set/get methods


> Source Menu > Generate Getters and Setters
(Or) alt+shift+S, R > SelectAll > OK

c. @Value uses Reflection based data read. So, no set/get method required.

----application.properties---
my.app.id=10
my.app.code=NIT
my.app.version=3.3
------------------------------
Runner class:

package in.nareshit.raghu;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("my.app")
public class MyConfigDataReader implements CommandLineRunner {

private Integer id;


private String code;
private Double version;

@Override
public void run(String... args) throws Exception {
System.out.println(this);
}

//alt+shift+S, S > Generate


@Override
public String toString() {
return "MyConfigDataReader [id=" + id + ", code=" + code + ", version="
+ version + "]";
}

//alt+shift+S, R > Select All > Generate


public Integer getId() {
return id;
}

public void setId(Integer id) {


this.id = id;
}

public String getCode() {


return code;
}

public void setCode(String code) {


this.code = code;
}

public Double getVersion() {


return version;
}

public void setVersion(Double version) {


this.version = version;
}

=> @ConfigurationProperties also supports working with Collections and


BeanType(classType)

a. List/Set/Array
For this concept we should use index (starts from zero) with below format
prefix.variable[index]=value
Ex: my.app.info[0]=SAMPLE

b. Map/Properties
For this concept we should use key=val format
prefix.variable.key=value
ex: my.app.data.G1=ACTIVE

c. Convertig to one class object


We can even read properties data into one class object
by using HAS-A relation and syntax is
prefix.objectName.variable=value
(or)
prefix.hasAVariable.variable=value

Q) What is the difference between Map and Properties?


A) Map(I) Generics Types are provided by Programmer and having multiple
Implementations.
Properties it is a class stores data with single format key=val (default
converted as String)

=======================Code=====================================
---application.properties---

my.app.id=10
my.app.code=NIT
my.app.version=3.3
my.app.info[0]=A
my.app.info[1]=B
my.app.info[2]=C

my.app.data.G1=ACTIVE
my.app.data.G2=NONE

my.app.dob.dtlId=101
my.app.dob.dtlCode=ABC

-------------------------
2. Model class

package in.nareshit.raghu;

public class Details {

private Integer dtlId;


private String dtlCode;

public Integer getDtlId() {


return dtlId;
}
public void setDtlId(Integer dtlId) {
this.dtlId = dtlId;
}
public String getDtlCode() {
return dtlCode;
}
public void setDtlCode(String dtlCode) {
this.dtlCode = dtlCode;
}

@Override
public String toString() {
return "Details [dtlId=" + dtlId + ", dtlCode=" + dtlCode + "]";
}

3. Runner class
package in.nareshit.raghu;

//ctrl+shift+O
import java.util.Arrays;
import java.util.Properties;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("my.app")
public class MyConfigDataReader implements CommandLineRunner {

private Integer id;


private String code;
private Double version;

//private List<String> info;


//private Set<String> info;
private String[] info;

//private Map<String,String> data;


private Properties data;

private Details dob; //HAS-A

@Override
public void run(String... args) throws Exception {
System.out.println(this);
}

//alt+shift+S, S > Generate


@Override
public String toString() {
return "MyConfigDataReader [id=" + id + ", code=" + code + ", version="
+ version + ", info="
+ Arrays.toString(info) + ", data=" + data + ", dob=" + dob
+ "]";
}

//alt+shift+S, R > Select All > Generate


public Integer getId() {
return id;
}

public void setId(Integer id) {


this.id = id;
}

public String getCode() {


return code;
}

public void setCode(String code) {


this.code = code;
}

public Double getVersion() {


return version;
}

public void setVersion(Double version) {


this.version = version;
}

public String[] getInfo() {


return info;
}

public void setInfo(String[] info) {


this.info = info;
}

public Properties getData() {


return data;
}

public void setData(Properties data) {


this.data = data;
}

public Details getDob() {


return dob;
}

public void setDob(Details dob) {


this.dob = dob;
}

4. Run Starter class

You might also like