Spring Boot--从配置文件中获取随机数

在配置文件中直接加入以下代码,我放到application.properties中的:


my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}


通过@Value注解在代码中获取:
@Component
public class MyBean implements CommandLineRunner{


    @Value("${random.value}")
    private String mySecret;


    @Value("${my.number}")
    private Integer myNumber;


    @Value("${my.bignumber}")
    private long myBignumber;


    @Value("${my.uuid}")
    private String myUuid;


    @Value("${my.number.less.than.ten}")
    private String myNumberLessThanTen;


    @Value("${my.number.in.range}")
    private String myNumberInRange;




    @Override
    public void run(String... args){
        System.out.println("mySecret:"+mySecret);
        System.out.println("myNumber:"+myNumber);
        System.out.println("myBignumber:"+myBignumber);
        System.out.println("myUuid:"+myUuid);
        System.out.println("myNumberLessThanTen:"+myNumberLessThanTen);
        System.out.println("myNumberInRange:"+myNumberInRange);
    }
}




参数间的引用
com.didispace.blog.name=程序猿DD
com.didispace.blog.title=Spring Boot教程
com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》

你可能感兴趣的:(springboot)