官方文档介绍
The RandomValuePropertySource is useful for injecting random values (e.g. into secrets or test cases). It can produce integers, longs, uuids or strings, e.g.
RandomValuePropertySource 类通常用来注入 int,long,uuid 和 string 类型的随机值。
The random.int* syntax is OPEN value (,max) CLOSE where the OPEN,CLOSE are any character and value,max are integers. If max is provided then value is the minimum value and max is the maximum (exclusive).
random.int* 的语法是 OPEN value (,max) CLOSE 。OPEN 和 CLOSE 可以是 任意字符,用来分隔方法参数。value, max 是 int 类型的整数。如果 max 参数存在,则 value 表示取值范围的最小值,max 表示最大值(不包含 max)。
编写配置文件
application.properties 文件配置:
my.value=${random.value}
my.int=${random.int}
my.long=${random.long}
my.uuid=${random.uuid}
my.int.less.than.ten=${random.int(10)}
my.int.in.range=${random.int[1024,65536]}
application.yml 文件配置:
my:
value: ${
random.value}
int: ${
random.int}
long: ${
random.long}
uuid: ${
random.uuid}
int.less.than.ten: ${
random.int(10)}
# 100前面和1000后面可以是 -,(,[ 等任意字符
int.in.range: ${
random.int-100,1000-}
编写 Controller 类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController