1. Random numbers
${random.int}
${random.int(max)}
${random.int(min,max)}
${random.long}
${random.long(max)}
${random.long(min,max)}
${random.uuid}
Random numbers are parsed using the RandomValuePropertySource class. Note: When using IDEA for random number configuration, the second and third random numbers of long will not be prompted. The blogger also found out when looking at RandomValuePropertySource .
public class RandomValuePropertySource extends PropertySource<Random> { /** * Name of the random {@link PropertySource}. */ public static final String RANDOM_PROPERTY_SOURCE_NAME = "random"; private static final String PREFIX = "random."; private static final Log logger = LogFactory.getLog(RandomValuePropertySource.class); public RandomValuePropertySource(String name) { super(name, new Random()); } public RandomValuePropertySource() { this(RANDOM_PROPERTY_SOURCE_NAME); } @Override public Object getProperty(String name) { if (!name.startsWith(PREFIX)) { return null; } if (logger.isTraceEnabled()) { logger.trace("Generating random property for '" + name + "'"); } return getRandomValue(name.substring(PREFIX.length())); } private Object getRandomValue(String type) { if (type.equals("int")) {//${random.int} //getSource() returns the actual object of the generic T, so nextInt() is to call the nextInt() method of the Random class return getSource().nextInt(); } if (type.equals("long")) {//${random.long} return getSource().nextLong(); } String range = getRange(type, "int"); if (range != null) { return getNextIntInRange(range); } range = getRange(type, "long"); if (range != null) { return getNextLongInRange(range); } if (type.equals("uuid")) {//${random.uuid} return UUID.randomUUID().toString(); } return getRandomBytes();//${random.value} } private String getRange(String type, String prefix) { if (type.startsWith(prefix)) { int startIndex = prefix.length() + 1; if (type.length() > startIndex) { return type.substring(startIndex, type.length() - 1); } } return null; } private int getNextIntInRange(String range) { String[] tokens = StringUtils.commaDelimitedListToStringArray(range); int start = Integer.parseInt(tokens[0]); if (tokens.length == 1) {//${random.int(value)} return getSource().nextInt(start); } //${random.int(value,max)} return start + getSource().nextInt(Integer.parseInt(tokens[1]) - start); } private long getNextLongInRange(String range) { String[] tokens = StringUtils.commaDelimitedListToStringArray(range); if (tokens.length == 1) {//${random.long(value)} return Math.abs(getSource().nextLong() % Long.parseLong(tokens[0])); } long lowerBound = Long.parseLong(tokens[0]); long upperBound = Long.parseLong(tokens[1]) - lowerBound; //${random.long(value,max)} return lowerBound + Math.abs(getSource().nextLong() % upperBound); } private Object getRandomBytes() { byte[] bytes = new byte[32]; getSource().nextBytes(bytes); //${random.value} return DigestUtils.md5DigestAsHex(bytes); } //... }
2. Placeholder
$ {aaa:default}, if ${aaa} does not exist, use default instead of the value of aaa; if $ {aaa} exists, use the value of $ {aaa}.
a = ${random.int} b = ${random.int(100)} c = ${random.int(500,1000)} d = ${random.long} e = ${random.long(10000)} f = ${random.long(20000,99999)} g = ${random.value} h = ${random.uuid} i = ${aaa:"bbb"}
, when referencing the i variable, the value of i will be parsed first, then the value of $ {aaa:"bbb"} will be parsed, and then the value of $ {aaa} will be parsed , if it exists, use the value of the aaa variable directly, if it does not exist, use "bbb" directly as the value of the a variable, which is actually a recursive analysis .i = ${aaa:"bbb"}
=====================Make an advertisement, welcome to pay attention =====================
QQ: |
412425870 |
WeChat public account: Cay Classroom |
|
csdn blog: |
http://blog.csdn.net/caychen |
Code cloud: |
https://gitee.com/caychen/ |
github: |
https://github.com/caychen |
Click on the group number or scan the QR code to join the QQ group: |
|
Click on the group number or scan the QR code to join the QQ group: |