Spring Boot Random Configuration Property Values

摘要: In this tutorial we demonstrate how to generate random property values for configuration properties using Spring Boot. The RandomValuePropertySource class is added – by spring boot – to the classpath. We can use it to produce random String, Integer, Long or UUID values.

In this tutorial we demonstrate how to generate random property values for configuration properties using Spring Boot. The RandomValuePropertySource class is added – by spring boot – to the classpath. We can use it to produce random String, Integer, Long or UUID values.

Maven Dependencies

We use Apache Maven to manage our projects dependencies. Make sure you add at least the following dependency to your project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.springboot</groupId>
    <artifactId>random-values</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <url>https://memorynotfound.com</url>
    <name>Spring Boot - ${project.artifactId}</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Random Configuration Property Values

We can produce random values in our configuration file. We can get a random value by using ${random.<type>} as a value for a configuration property. At the moment, only String, Integer, Long or UUID types are supported. You can optionally add a range filter at the end e.g.: ${random.int[<start>,<end>]}.

my:
  secret:
    password: ${random.value}
    intValue: ${random.int}
    intValueRange: ${random.int[1,99]}
    longValue: ${random.long}
    longValueRange: ${random.long[111111111111,999999999999]}
    uuid: ${random.uuid}

Spring Boot – Get Random Property Values

That’s it for the configuration, the RandomValuePropertySource will automatically inject the random value into the configuration file. Once our application boots, we can simple get those generated configuration properties. We demonstrate this using the following code.

package com.memorynotfound.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import java.util.UUID;

@SpringBootApplication
public class Application {

    private static Logger log = LoggerFactory.getLogger(Application.class);

    @Value("${my.secret.password}")
    private String password;

    @Value("${my.secret.intValue}")
    private Integer intValue;

    @Value("${my.secret.intValueRange}")
    private Integer intValueRange;

    @Value("${my.secret.longValue}")
    private Long longValue;

    @Value("${my.secret.longValueRange}")
    private Long longValueRange;

    @Value("${my.secret.uuid}")
    private UUID uuid;

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

    @PostConstruct
    private void init(){
        log.info("Configure Random Property Values using Spring Boot");
        log.info("password: " + password);
        log.info("intValue: " + intValue);
        log.info("intValueRange: " + intValueRange);
        log.info("longValue: " + longValue);
        log.info("longValueRange: " + longValueRange);
        log.info("uuid: " + uuid);
    }

}

Output

The previous application simply prints the output to the console.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2017-05-29 11:48:24.143  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : Configure Random Property Values using Spring Boot
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : password: 41a84b2a3065083822dc27e0d960a782
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : intValue: -228998254
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : intValueRange: 77
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : longValue: -696529864237843521
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : longValueRange: 505141632395
2017-05-29 11:48:24.144  INFO 7320 --- [           main] c.memorynotfound.springboot.Application  : uuid: d46ce465-2ab0-479f-b8e6-7ea37b5647a7
...

Download

上一篇: Spring Boot Passing Command Line Arguments Example
下一篇: Spring Boot – Create Executable using Maven without Parent Pom
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号