Spring Boot支持在系统加载的时候配置随机数。

添加config/random.properties文件,添加以下内容:

#随机32位MD5字符串
user.random.secret=${random.value} #随机int数字
user.random.intNumber=${random.int} #随机long数字
user.random.longNumber=${random.long} #随便uuid
user.random.uuid=${random.uuid} #随机10以内的数字
user.random.lessTen=${random.int(10)} #随机1024~65536之内的数字
user.random.range=${random.int[1024,65536]}

添加绑定类:

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 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;
} }

输出如下:

secret=83a5c3402ef936a37842dc6de3d1af0f
intNumber=1816149855
lessTen=1
range=37625
longNumber=8449008776720010146
uuid=e5bc2091-1599-45b1-abd7-e3721ac77e6b

具体的生成细节可以参考Spring Boot的配置类:

org.springframework.boot.context.config.RandomValuePropertySource

来看下它的源码,实现其实很简单。

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 getNextIntInRange(range);
}
range = getRange(type, "long");
if (range != null) {
return getNextLongInRange(range);
}
if (type.equals("uuid")) {
return UUID.randomUUID().toString();
}
return getRandomBytes();
}

其实就是使用了 Java 自带的 java.util.Randomjava.util.UUID 等工具类,实现很简单,这里就不再详细解析了,大家可以自己去看下这个类的实现。

随机数的生成配置就是这么点了,我知道的是可以随机生成应用程序端口,其他的还真没用到。

@程序猿 你们还知道其他的随机数应用么?

推荐:Spring Boot & Cloud 最强技术教程

Spring Boot 配置随机数技巧的更多相关文章

  1. Spring Boot配置随机数

    Spring Boot支持在系统加载的时候配置随机数. 添加config/random.properties文件,添加以下内容: #随机32位MD5字符串 user.random.secret=${r ...

  2. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  3. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  4. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  5. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  6. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  7. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  8. [转] Spring Boot配置多个DataSource

    [From]  https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...

  9. Spring boot 配置异步处理执行器

    示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

随机推荐

  1. C++中如何对输出几位小数进行控制(setprecision)

  2. AngularJS学习笔记(二)

    一.AngularJS Select(选择框) 1.使用 ng-options 创建选择框 <div ng-app="myApp" ng-controller="m ...

  3. springboot和mybatis之thymleaf整合简单插入用户数据

    编写mapper接口和对应的mapper.xml文件,注意对应的注解 @Mapper @Repository public interface StudentMapper { void insertS ...

  4. jquery中bind和on的区别

    1.首先,来看看bind和on的语法. bind的用法: $('a').bind('click',[data],function(){}) 其事件的绑定者是固定的,就是a,第一个参数是事件,第二个参数 ...

  5. 4412 uboot启动分析

    感谢sea1105, https://blog.csdn.net/sea1105/article/details/52142772 在学习过程中,由于tiny4412资料太过于少,因此参考210的视屏 ...

  6. 3P - Snooker

    background: Philip likes to play the QQ game of Snooker when he wants a relax, though he was just a ...

  7. MySQL优化(四) 慢查询的定位及优化

    一.SQL语句优化的一般步骤: (1)通过 show status 命令了解各种 SQL 的执行效率: (2)定位执行效率较低的 SQL 语句(重点是 Select): (3)通过 explain 分 ...

  8. docker 支持ipv6 (核心要点是ndp需要把docker内的ip全部加入到ndplist中来)

    IPv6 with Docker Estimated reading time: 10 minutes The information in this section explains IPv6 wi ...

  9. Failed to connect to 127.0.0.1 port 1080: Connection refused package 问题解决方法

    错误: fatal: unable to access 'https://github.com/******': Failed to connect to 127.0.0.1 port 1080: C ...

  10. linux命令-crontab

    一.安装 yum install crontabs 二.基本使用 1.crontab -e:创建任务,进入编辑 格式: 基本格式 : ——————————————————— * * * * * com ...