Spring Boot 中,random.* 随机值

在 Spring Boot 中,random.* 相关的属性确实有一定的范围,但主要包括以下几种类型:

random.int: 生成一个随机整数。
random.long: 生成一个随机长整数。
random.uuid: 生成一个随机 UUID(通用唯一标识符)。
random.boolean: 生成一个随机布尔值 (true 或 false)。
random.alphanumeric: 生成一个指定长度的随机字母数字字符串。
random.pseudopassword: 生成一个伪密码,通常用于模拟密码场景。

  1. random.int
    配置:

在 application.properties 文件中:

properties
random.int=1000

在 application.yml 文件中:

yaml

random:
  int: 1000

说明: 生成一个 0 到 1000 之间的随机整数。这个属性在配置文件中设置了一个范围的上限。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomIntComponent {

    @Value("${random.int}")
    private int randomInt;

    public void printRandomInt() {
        System.out.println("Random Integer: " + randomInt);
    }
}
  1. random.long
    配置:

在 application.properties 文件中:

properties
random.long=10000000000

在 application.yml 文件中:

yaml

random:
  long: 10000000000

说明: 生成一个 0 到 10000000000 之间的随机长整数。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomLongComponent {

    @Value("${random.long}")
    private long randomLong;

    public void printRandomLong() {
        System.out.println("Random Long: " + randomLong);
    }
}
  1. random.uuid
    配置:

在 application.properties 文件中:

properties
random.uuid=true

在 application.yml 文件中:

yaml

random:
  uuid: true

说明: 生成一个随机 UUID(通用唯一标识符)。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomUuidComponent {

    @Value("${random.uuid}")
    private String randomUuid;

    public void printRandomUuid() {
        System.out.println("Random UUID: " + randomUuid);
    }
}
  1. random.boolean
    配置:

在 application.properties 文件中:

properties
random.boolean=true

在 application.yml 文件中:

yaml

random:
  boolean: true

说明: 生成一个随机布尔值 (true 或 false)。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomBooleanComponent {

    @Value("${random.boolean}")
    private boolean randomBoolean;

    public void printRandomBoolean() {
        System.out.println("Random Boolean: " + randomBoolean);
    }
}
  1. random.alphanumeric
    配置:

在 application.properties 文件中:

properties

random.alphanumeric=8

在 application.yml 文件中:

yaml

random:
  alphanumeric: 8

说明: 生成一个长度为 8 的随机字母数字字符串。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomAlphanumericComponent {

    @Value("${random.alphanumeric}")
    private String randomAlphanumeric;

    public void printRandomAlphanumeric() {
        System.out.println("Random Alphanumeric: " + randomAlphanumeric);
    }
}
  1. random.pseudopassword
    配置:

在 application.properties 文件中:

properties

random.pseudopassword=12

在 application.yml 文件中:

yaml

random:
  pseudopassword: 12

说明: 生成一个长度为 12 的伪密码(通常是一个复杂的随机字符串)。

在代码中使用:

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RandomPseudopasswordComponent {

    @Value("${random.pseudopassword}")
    private String randomPseudopassword;

    public void printRandomPseudopassword() {
        System.out.println("Random Pseudopassword: " + randomPseudopassword);
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值