为什么我们需要配置随机值?例如,我们想让我们的应用每次启动均使用不同的端口,然后自动打开浏览器访问该应用,这样将避免由于端口冲突导致应用启动失败。Spring Boot 支持在配置文件中生成随机整数、随机字符串和 UUID,并且随机整数允许控制在指定的范围之内。下面将详解介绍 Spring Boot 配置文件对随机值的支持。
该 Spring Boot 的 pom.xml 依赖如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.13</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hxstrive</groupId> <artifactId>springboot2_demos_demo01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot2_demos_demo01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
修改 application.yml 配置文件,内容如下:
hxstrive: # 随机整数 value1: ${random.int} # 10以内的随机数 value2: ${random.int(10)} # 10到20之间的随机数 value3: ${random.int(10,20)} # 随机字符串,MD5字符串,32位 value4: ${random.value} # 随机UUID value5: ${random.uuid} # 随机整数(范围 long) value6: ${random.long} # 随机整数(范围:10~100) value7: ${random.long(10, 100)}
package com.hxstrive.springboot2_demos_demo01; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @SpringBootApplication public class Springboot2DemosDemo01Application { // 注入值 @Value("${hxstrive.value1}") private String value1; @Value("${hxstrive.value2}") private String value2; @Value("${hxstrive.value3}") private String value3; @Value("${hxstrive.value4}") private String value4; @Value("${hxstrive.value5}") private String value5; @Value("${hxstrive.value6}") private String value6; public static void main(String[] args) { SpringApplication.run(Springboot2DemosDemo01Application.class, args); } @GetMapping("/") public String index() { System.out.println("value1=" + value1); System.out.println("value2=" + value2); System.out.println("value3=" + value3); System.out.println("value4=" + value4); System.out.println("value5=" + value5); System.out.println("value6=" + value6); return new Date().toString(); } }
运行应用,使用浏览器访问 http://localhost:8080,控制台输出如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.13) ... 2023-07-22 10:36:59.718 INFO 1292 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms value1=-885549549 value2=8 value3=16 value4=8256b006362bf893139358f4f0e06223 value5=bc930440-002f-479d-9302-c44348acf13c value6=-6320840787392360533