第3章 Spring Boot 的系统配置
3.1 Spring Boot 系统配置文件
3.2 Spring Boot 自定义配置项
3.3 Spring Boot 其他配置
3.4 Spring Boot 日志配置
3.5 实战:Spring Boot 实现系统多环境配置
3.3 Spring Boot 其他配置
Spring Boot 支持很多实用的参数配置功能。本节介绍 Spring Boot 配置生成随机数、配置引用等实际项目中使用配置。
3.3.1 随机数
Spring Boot 提供的 RandomValuePropertySource
配置类可以很方便地生成随机数,可以生成 int、long、UUID 和 String 类型的数据。
首先,在 application.properties 中添加随机数的配置项。
application.properties
# 3.3.1 随机数
# 随机字符串
cfg.random.value=${random.value}
# uuid
cfg.random.uuid=${random.uuid}
# 随机 int
cfg.random.number=${random.int}
# 随机 long
cfg.random.bignumber=${random.long}
# 10 以内的随机数
cfg.random.test1=${random.int(10)}
# 10-20 的随机数
cfg.random.test2=${random.int[10,20]}
然后,创建配置映射类 ConfigRandomValue 获取随机数配置项,读取配置项的方式和读取普通配置项的方式一样:通过 @Value 或者 @ConfigurationProperties 注解来读取。
ConfigRandomValue.java
package com.example.helloworld.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 配置映射类 ConfigRandomValue
*
* 创建配置映射类 ConfigRandomValue 获取随机数配置项,读取配置项的方式和读取普通配置项的方式一样:通过 @Value 或者 @ConfigurationProperties 注解来读取。
*/
@Component
public class ConfigRandomValue {
@Value("${cfg.random.value}")
private String value;
@Value("${cfg.random.uuid}")
private String uuid;
@Value("${cfg.random.number}")
private String number;
@Value("${cfg.random.bignumber}")
private String bignumber;
@Value("${cfg.random.test1}")
private String test1;
@Value("${cfg.random.test2}")
private String test2;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBignumber() {
return bignumber;
}
public void setBignumber(String bignumber) {
this.bignumber = bignumber;
}
public String getTest1() {
return test1;
}
public void setTest1(String test1) {
this.test1 = test1;
}
public String getTest2() {
return test2;
}
public void setTest2(String test2) {
this.test2 = test2;
}
}
最后,验证随机数配置是否生效。创建单元测试,验证随机数是否生成成功。
ConfigRandomValueTest.java
package com.example.helloworld.model;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ConfigRandomValueTest {
@Autowired
private ConfigRandomValue randomValue;
@Test
void getRandom() {
System.out.println(randomValue.getValue()); //输出示例:ff388d181b59972a8a81b8cea1c9e404
System.out.println(randomValue.getUuid()); //输出示例:99a0f66e-5423-4d16-bf55-213a9e3c6cd3
System.out.println(randomValue.getNumber()); //输出示例:-546856626
System.out.println(randomValue.getBignumber()); //输出示例:6448152719803819243
System.out.println(randomValue.getTest1()); //输出示例:8
System.out.println(randomValue.getTest2()); //输出示例:18
}
}
在上面的示例中,首先注入了自定义的随机数生成配置类,然后调用了相关的配置项来验证随机数规则是否生效。
3.3.2 配置引用
Spring Boot 支持使用占位符获取之前的属性配置,也就是在后一个配置的值中直接引用先前定义过的配置项,直接解析其中的值。
这样做的好处是:在多个具有相互关联的配置项中,只需要对其中一处配置项预先设置,其他地方都可以引用,省去了后续多出修改的麻烦。
使用格式:${name},name 表示先前在配置文件中已经设置过的配置项名。下面通过示例演示如何在配置文件
中实现参数引用
。
application.properties
# 3.3.2 配置引用
my.name=ZhangSan
my.sex=1
my.des=My name is ${my.name}.
# ${my.name}还可以使用“:”指定默认值(只能在配置文件中使用,在@Value注解、Environment等方式中无效),避免没有配置参数导致程序异常,示例代码如下:
#my.des=My name is ${my.name:LiSi}.
创建单元测试,验证配置引用是否生效。
configRefTest.java
package com.example.helloworld.otherConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
/**
* 测试配置引用
*/
@SpringBootTest
public class configRefTest {
@Value("${my.name}")
private String name;
@Value("${my.sex}")
private int sex;
@Value("${my.des}")
private String des;
@Test
void getConfigRef() {
System.out.println(name); //输出结果:ZhangSan
System.out.println(sex); //输出结果:1
System.out.println(des); //输出结果:My name is ZhangSan.
}
}
来源:《Spring Boot 从入门到实战》学习笔记