springboot使用随机数

在配置文件中,还提供了随机数供我们使用,即在配置文件中使用${random}来生成不同类型的随机数,大致分为随机数、随机uuid、随机字符串等。在配置文件内添加几种利用随机数创建的属性,如代码

server.port=8089
book.name=spring boot 2
book.author=yangyang
#随机字符串
book.value=${random.value}
#随机int值
book.intValue=${random.int}
#随机long值
book.longValue=${random.long}
#随机uuid
book.uuid=${random.uuid}
#随机1000以内
book.randomNumber=${random.int(1000)}
#自定义属性引用
book.title= The book name is ${book.name}

在配置了这么多属性后,可以使用JavaBean模式来给属性赋值,创建一个BookConfigBean实体类。

由于自定义属性的前缀都是由book开头的,因此我们可以在实体类上加入注解@ConfigurationProperties(prefix = "book"),同时需要在启动类上加入注解@EnableConfigurationProperties(BookConfigBean.class),表明启动这个配置类。

package com.shrimpking;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties(BookConfigBean.class)
@SpringBootApplication
public class Springboot69Application
{

    public static void main(String[] args)
    {
        SpringApplication.run(Springboot69Application.class, args);
    }

}

实体类内容如代码

package com.shrimpking;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/12/20 22:33
 */
@ConfigurationProperties(prefix = "book")
public class BookConfigBean
{

    private String name;
    private String author;
    private String value;
    private int intValue;
    private long longValue;
    private String uuid;
    private int randomNumber;
    private String title;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

    public int getIntValue()
    {
        return intValue;
    }

    public void setIntValue(int intValue)
    {
        this.intValue = intValue;
    }

    public long getLongValue()
    {
        return longValue;
    }

    public void setLongValue(long longValue)
    {
        this.longValue = longValue;
    }

    public String getUuid()
    {
        return uuid;
    }

    public void setUuid(String uuid)
    {
        this.uuid = uuid;
    }

    public int getRandomNumber()
    {
        return randomNumber;
    }

    public void setRandomNumber(int randomNumber)
    {
        this.randomNumber = randomNumber;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }
}

到这里,配置就完成了。接下来在Controller中利用@Autowired注解注入BookConfigBean类,并且创建一个test2方法进行测试。test2方法及注入BookConfigBean类的内容如代码

package com.shrimpking;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/12/20 22:36
 */
@RestController
public class BookController
{
    @Autowired
    private BookConfigBean bookConfigBean;

    @GetMapping("/test2")
    public BookConfigBean test2(){
        return bookConfigBean;
    }
}

在浏览器上访问http://localhost:8089/test2进行测试,显示结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值