当前位置:网站首页>Random number setting and reference between parameters
Random number setting and reference between parameters
2022-07-29 09:25:00 【Leisurely summer】
stay Spring Boot When setting properties in the configuration file , In addition to the configuration property values shown in the previous example , You can also use random values and references between parameters to set attribute values . below , This paper explains how to set these two property values in the configuration file
1、 Random value setting
stay Spring Boot In profile , Random value setting uses Spring Boot embedded RandomValuePropertySource class , Inject random values into some hidden attribute values or test case attribute values. The syntax format of random values is ${random.xx},xx Indicates that the type and range of generated random numbers need to be specified , It can generate random integers 、uuid Or a string , The sample code is as follows
my.secret=${random.value} // Configure random values
my.number=${random.int} // Configure random integers
my.bignumber=${random.long} // Configure random long Number of types
my.uuid=${random.uuid} // Configure random uuid Number of types
my.number.less.than.ten=${random.int(10)} // Configuration less than 10 Random integer of
my.number.in.range=${random.int[1024,65536]} // The configuration range is [1024,65536] Random integer between
In the above code , Use RandomValuePropertySource Class random The random number type provided , Examples of setting different types of random values are shown
2、 Reference between parameters
stay Spring Boot In profile , The property values of configuration files can also be referenced between parameters , That is, the previously defined attribute is directly referenced in the attribute value of the latter configuration , In this way, the attribute values can be directly parsed .
The advantage of using references between parameters is , In multiple configuration properties that are associated with each other , You only need to pre configure one of these properties , Other places can quote , It saves the trouble of many subsequent modifications .
The syntax format of references between parameters is ${xx},xx Represents the property name that has been previously configured in the configuration file , The sample code is as follows
app.name=MyApp
app.description=${app.name} is a Spring Boot application
In the example of reference setting between the above parameters , First set up “app.name=MyApp”, take app.name The property value of the property is set to MyApp; next , stay app.description Property configuration , Use ${app.name} The previous attribute value is referenced
Next , Through a case to demonstrate the specific use and effect of property setting by using random value setting and reference between parameters , The specific steps are as follows
(1) open Spring Boot project resources In the catalog application.properties The configuration file , In this configuration file, two test attributes are configured through random value setting and reference between parameters , The sample code is as follows
# Random value setting and parameter reference configuration
tom.age=${random.int[10,20]}
tom.description=tom My age may be ${tom.age}
In the above application.properties In profile , First use random values to set tom.age Property value of property , The attribute value is set to [10,20] Between , Then we use the reference between parameters to configure tom.description attribute
(2) open \ Test class of the project , Add string type in this test class description attribute , And in the configuration file tom.description Property to inject , Then add a new test method for output test , The sample code is as follows
@Value("${tom.description}")
private String description;
@Test
public void placeholderTest() {
System.out.println(description);
}In the above code , adopt @Value("${tom.description}") The annotation will be in the configuration file tom.description Property values are injected into the corresponding description Properties of the , In the test method placeholderTest() The attribute value is printed out in .
Perform test methods placeholderTest() , Check the console output

It can be seen that , The test method placeholderTest() The successful running , And print out the attributes description The content of the injection , This content is consistent with the property values configured in the configuration file . next , Repeat the test method placeholderTest(), The age displayed in the output statement of the console will be [10,20] Random display between
边栏推荐
- CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo
- File upload and expansion
- Vs2015 uses loadlibrary to call DLL library
- 不用Swagger,那我用啥?
- WebAssembly 2022 问卷调查结果新鲜出炉
- dataframe. to_ Sql() inserts too many errors at one time
- ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- [unity entry program] collection of common learning websites
- Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
- Solve the problem of reading data garbled by redis visualization tool
猜你喜欢

Could not receive a message from the daemon

How to introduce your project experience?

Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in

WebAssembly 2022 问卷调查结果新鲜出炉
Data representation and calculation (base)

Leetcode:132. split palindrome string II

存算一体与存内计算计算杂谈

数仓项目踩坑记录与解决方法总结
![[unity entry program] C # and unity - understand classes and objects](/img/bd/13cc90638c6e6ad4a45507b0ed2fb7.png)
[unity entry program] C # and unity - understand classes and objects

MySQL converts some table names to uppercase
随机推荐
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
存算一体与存内计算计算杂谈
Database system design: partition
Tesseract text recognition -- simple
MySQL converts some table names to uppercase
Floweable foundation Chapter 1
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
Nutnews developed based on arkui ETS
Shutter gradient
On contract testing
The use and Simulation of string function, character function and memory function
网络原理笔记(五层网络)
Quick sorting (quick sorting) (implemented in C language)
怎么样的框架对于开发者是友好的?
How to introduce your project experience?
AI is at the forefront | focusing on natural language processing, machine learning and other fields; From Fudan University, Institute of automation, Chinese Academy of Sciences and other teams
On the charm of code language
【Unity入门计划】常用学习网址收藏
dataframe. to_ Sql() inserts too many errors at one time
[C language] DataGridView binding data