Spring Boot supports configuring random numbers when the system is loading.
Add the config/random.properties file and add the following:
#Random 32-bit MD5 string user.random.secret=${random.value}#Random int number user.random.intNumber=${random.int}#Random long number user.random.longNumber=${random.long}#Random uuiduser.random.uuid=${random.uuid}#Random 10 number user.random.lessTen=${random.int(10)}#Random 1024~65536 user.random.range=${random.int[1024,65536]}Add binding class:
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "user.random")@PropertySource(value = { "config/random.properties" })public class RandomConfig { private String secret; private int intNumber; private int lessTen; private int range; private long long longNumber; private String uuid; public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public int getIntNumber() { return intNumber; } public void setIntNumber(int intNumber) { this.intNumber = intNumber; } public int getLessTen() { return lessTen; } public void setLessTen(int lessTen) { this.lessTen = lessTen; } public int getRange() { return range; } public void setRange(int range) { this.range = range; } public long getLongNumber() { return longNumber; } public void setLongNumber(long longNumber) { this.longNumber = longNumber; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; }}The output is as follows:
secret=83a5c3402ef936a37842dc6de3d1af0f
intNumber=1816149855
lessTen=1
range=37625
longNumber=8449008776720010146
uuid=e5bc2091-1599-45b1-abd7-e3721ac77e6b
For specific generation details, please refer to the Spring Boot configuration class:
org.springframework.boot.context.config.RandomValuePropertySource
Let’s take a look at its source code, the implementation is actually very simple.
public RandomValuePropertySource(String name) { super(name, new Random());}private Object getRandomValue(String type) { if (type.equals("int")) { return getSource().nextInt(); } if (type.equals("long")) { return getSource().nextLong(); } String range = getRange(type, "int"); if (range != null) { return getNextInRange(range); } range = getRange(type, "long"); if (range != null) { return getNextLongInRange(range); } if (type.equals("uuid")) { return UUID.randomUUID().toString(); } return getRandomBytes();} In fact, it uses the Java-owned java.util.Random and java.util.UUID and other tool classes. The implementation is very simple. I won’t analyze it in detail here. You can check the implementation of this class by yourself.
This is the configuration for random numbers. What I know is that the application port can be generated randomly, but the others are really useless.
Summarize
The above is the Spring Boot configuration random number skills introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!