前言
上节我们讲到了用bean
读取配置文件application.yml
中的自定义属性,大家有兴趣的话,可参看以下文章:
SpringBoot(3) — application.yml自定义属性赋值给bean
在Springboot
的开发中,我们偶尔会使用到随机数的问题。那么今天就让我们来学习下随机数Random
在配置文件application.yml
中的使用吧。
今天涉及知识有:
- 配置文件application.yml中random的用法
- 在application.yml中自定义属性
- bean 接收application.yml中自定义属性
- Controller中调用bean
- 运行验证
先来波效果图

一. 配置文件application.yml中random的用法
在配置文件application.yml
中,我们偶尔会使用到随机数Random
。 Spring Boot
的属性配置文件中可以通过${random}
来产生int
值、long
值、string
字符串或者UUID
,来支持属性的随机值。Random
常用随机方法有以下几种:
random.value 随机字符串
random.uuid 随机uuid
random.int(90) 90以内随机数
random.int[30,70] [30,70]之间随机数
random.long(10000) 10000以内随机long值
二. 在application.yml中自定义属性
下面在配置文件application.yml
中新建testrandom
标签,并建立几个属性,赋上随机数值。application.yml
代码如下:
网友评论