随机数的作用:
(1)防止浏览器认为是相同的资源而去读缓存;
(2)生成验证码的时候可以使用;
步骤:
(1)application.properties下配置
number=${random.int}
random.int意为随机整数,分别还有random.value--随机字符串,random.long--随机long,random.int(10)--小于10,random.int(10,100)--10-100
(2)controller如下:
@Value("${number}")
private Integer number;
@RequestMapping(value = "/test")
String home(){
return "hello java"+":"+number;
}
@Value注解读取的properties配置的number,即把random.int赋值给了属性,类型Integer的number。
(3)通过/test方法
返回:hello java:-704069132
注:尽量不要在同一个方法上测试(比如在test方法上测试随机值--不同的功能用不同的方法,另外为了测试随机值更改了test也是不合适的)。