The
RandomValuePropertySource is useful for injecting random values. You can
generate random integers, longs, uuids, or strings.
ConfigBean.java
Application.java
When you ran 'Application.java', you can able to see below messages in console.
Example
app.secret=${random.value}
app.number=${random.int}
app.bignumber=${random.long}
app.uuid=${random.uuid}
app.number.less.than.fifty=${random.int(50)}
app.number.in.range=${random.int[500,1000]}
You
can inject them in java application like below.
@Value("${app.secret}")
private
String secret;
@Value("${app.number}")
private
int number;
@Value("${app.bignumber}")
private
long bigNumber;
@Value("${app.uuid}")
private
String uuid;
@Value("${app.number.less.than.fifty}")
private
int numberLessThan50;
@Value("${app.number.in.range}")
private
int numberInRange;
Find the below
working example
application.properties
app.secret=${random.value} app.number=${random.int} app.bignumber=${random.long} app.uuid=${random.uuid} app.number.less.than.fifty=${random.int(50)} app.number.in.range=${random.int[500,1000]}
ConfigBean.java
package com.sample.myApp.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConfigBean { @Value("${app.secret}") private String secret; @Value("${app.number}") private int number; @Value("${app.bignumber}") private long bigNumber; @Value("${app.uuid}") private String uuid; @Value("${app.number.less.than.fifty}") private int numberLessThan50; @Value("${app.number.in.range}") private int numberInRange; public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public long getBigNumber() { return bigNumber; } public void setBigNumber(long bigNumber) { this.bigNumber = bigNumber; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public int getNumberLessThan50() { return numberLessThan50; } public void setNumberLessThan50(int numberLessThan50) { this.numberLessThan50 = numberLessThan50; } public int getNumberInRange() { return numberInRange; } public void setNumberInRange(int numberInRange) { this.numberInRange = numberInRange; } }
Application.java
package com.sample.myApp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.sample.myApp.model.ConfigBean; import org.springframework.boot.context.config.RandomValuePropertySource; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); ConfigBean configBean = applicationContext.getBean(ConfigBean.class); long bigNumber = configBean.getBigNumber(); int number = configBean.getNumber(); int numberInRange = configBean.getNumberInRange(); int numberLessThan50 = configBean.getNumberLessThan50(); String secret = configBean.getSecret(); String uuid = configBean.getUuid(); System.out.println("******************************"); System.out.printf("bigNumber : %d \n", bigNumber); System.out.printf("number : %d \n", number); System.out.printf("numberInRange : %d \n", numberInRange); System.out.printf("numberLessThan50 : %d \n", numberLessThan50); System.out.printf("secret : %s \n", secret); System.out.printf("uuid : %s \n", uuid); System.out.println("******************************"); SpringApplication.exit(applicationContext); } }
When you ran 'Application.java', you can able to see below messages in console.
****************************** bigNumber : -3064097796043813516 number : -110057729 numberInRange : 660 numberLessThan50 : 4 secret : cc970482bdd5f1f3155019c7016904dc uuid : c069726a-c605-4dc4-84a1-761fc02acc59 ******************************
No comments:
Post a Comment