Random number setting and parameter reference

Posted Jun 16, 20205 min read

1.8 Random number setting and parameter reference

When setting properties in the Spring Boot configuration file, in addition to the configuration property values shown in the previous example, you can also use random values and parameter references to set property values. The following explains the setting methods of these two attribute values in the configuration file

1.8.1 Random value setting

In the Spring Boot configuration file, the random value setting uses the RandomValuePropertySource class embedded in Spring Boot, and random value injection is performed on some hidden property values or test case property values

The syntax format for setting the random value is ${random.xx}, xx indicates that you need to specify the type and range of the generated random number. It can generate a random integer, uuid, or string. The sample code is as follows

properties

my.secret=${random.value} //Configure random value

my.number=${random.int} //Configure random integer

my.bignumber=${random.long} //Configure random long type number

my.uuid=${random.uuid} //Configure random uuid type number

my.number.less.than.ten=${random.int(10)} //Configure random integer less than 10

my.number.in.range=${random.int[1024,65536]} //Random integer with a configuration range of [1024,65536]

In the above code, the random number type provided by random in the RandomValuePropertySource class is used to show examples of setting different types of random values

1.8.2 Reference between parameters

In the Spring Boot configuration file, the attribute value of the configuration file can also be referenced between parameters, that is, the attribute value that has been previously defined is directly referenced in the attribute value of the latter configuration, so that the attribute value can be directly parsed.

The advantage of using reference between parameters is that among multiple configuration attributes that are related to each other, only one of the attributes needs to be pre-configured, and other places can be referenced, saving the trouble of subsequent multiple modifications

The syntax format quoted between the parameters is ${xx}, xx represents the attribute name that has been previously configured in the configuration file, the sample code is as follows

properties

app.name=MyApp

app.description=${app.name} is a Spring Boot application

In the above reference setting example between parameters, first set "app.name=MyApp", set the attribute value of the app.name attribute to MyApp; then, in the app.description attribute configuration, use ${app.name} pair The previous attribute value is referenced

Next, through a case to demonstrate the specific use and effect of using random value settings and parameter references to set properties, the specific steps are as follows

(1) Open the application.properties configuration file in the Resources directory of the Spring Boot project, and configure two test properties through random value settings and parameter references in the configuration file. The sample code is as follows

properties

Random value setting and reference configuration between parameters

tom.age=${random.int[10,20]}

tom.description=tom's age may be ${tom.age}

In the above application.properties configuration file, first set the property value of the tom.age property with a random value, which is set between [10,20], and then use the parameter reference to configure tom.description Attributes

(2) Open the test class of the \ project, add a description attribute of the string type to the test class, and inject the tom.description attribute in the configuration file, and then add a test method to output the test, example code show as below

java

@Value("${tom.description}")

private String description;

@Test

public void placeholderTest() {

System.out.println(description);

}

In the above code, the value of the tom.description attribute in the configuration file is injected into the corresponding description attribute through the @Value("${tom.description}") annotation, and the attribute value is output in the test method placeholderTest() print.

Execute the test method placeholderTest() to view the console output effect

image-20191225172004276

It can be seen that the test method placeholderTest() runs successfully and prints out the injection content of the attribute description, which is consistent with the attribute values configured in the configuration file. Then, repeatedly execute the test method placeholderTest(), and the age displayed in the console output statement will be randomly displayed between [10,20]

  1. SpringBoot principle in-depth and source code analysis

    The traditional Spring framework implements a Web service, you need to import various dependent JAR packages, and then write the corresponding XML configuration file. Compared with Spring Boot, Spring Boot is more convenient, fast and efficient. So, how exactly does Spring Boot do this?

Next, in-depth analysis of the dependency management, automatic configuration and execution processes of the Spring Boot framework

2.1 Dependency management

Question:(1) Why is it not necessary to specify the version when importing dependency?

In the Spring Boot entry program, the project pom.xml file has two core dependencies, namely spring-boot-starter-parent and spring-boot-starter-web. The relevant introductions about these two dependencies are as follows:

  1. spring-boot-starter-parent depends on **

Find the spring-boot-starter-parent dependency in the pom.xml file in the chapter01 project. The sample code is as follows:

xml

org.springframework.boot

spring-boot-starter-parent<11./artifactId>

2.2.2.RELEASE

In the above code, the spring-boot-starter-parent dependency is used as the unified parent project dependency management of the Spring Boot project, and the project version number is unified as 2.2.2.RELEASE, which can be modified according to actual development needs

Use "Ctrl + left mouse button" to enter and view the underlying source file of spring-boot-starter-parent, and found that the underlying layer of spring-boot-starter-parent has a parent dependency spring-boot-dependencies, the core code is as follows

xml

org.springframework.boot

spring-boot-dependencies

2.2.2.RELEASE

../../spring-boot-dependencies

Continue to view the underlying source files of spring-boot-dependencies, the core code is as follows:

xml

<activemq.version>5.15.11</activemq.version>

...

<solr.version>8.2.0</solr.version>

<mysql.version>8.0.18</mysql.version>

<kafka.version>2.3.1</kafka.version>

<spring-amqp.version>2.2.2.RELEASE</spring-amqp.version>

<spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version>

<spring-retry.version>1.2.4.RELEASE</spring-retry.version>

<spring-security.version>5.2.1.RELEASE</spring-security.version>

<spring-session-bom.version>Corn-RELEASE</spring-session-bom.version>

<spring-ws.version>3.0.8.RELEASE</spring-ws.version>

<sqlite-jdbc.version>3.28.0</sqlite-jdbc.version>

<sun-mail.version>${jakarta-mail.version}</sun-mail.version>

<tomcat.version>9.0.29</tomcat.version>

<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>

<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>

...

As can be seen from the underlying source file of spring-boot-dependencies, this file uses the tag to perform unified version number management on some common technical framework dependency files, such as activemq, spring, tomcat, etc., all with Spring Boot 2.2. The 2 version matches the version, which is why the introduction of the dependency file in pom.xml does not need to mark the version number of the dependency file.

It should be noted that if the dependency file introduced by pom.xml is not managed by spring-boot-starter-parent, then when introducing the dependency file in pom.xml, you need to use the tag to specify the version number of the dependency file.

I just learned the "Java Engineer High Salary Training Camp" of Lagou Education, and I answered when I saw what I just learned. I hope Lagou can push me to the company I want to go to. Goal:byte! !