本文介绍 Spring Boot 2 配置数组、Map、随机值及转义字符。
目录
- 开发环境
- 配置数组(
Array
|List
|Set
) - 配置 Map
- 配置随机值
- 配置转义字符
开发环境
- Oracle JDK 1.8.0_201
- Apache Maven 3.6.0
- IntelliJ IDEA (Version 2018.3.3)
配置数组(Array
| List
| Set
)
-
创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程。
-
生成的
pom
文件如下,添加spring-boot-configuration-processor
和junit
依赖。
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>tutorial.spring.boot</groupId>
<artifactId>spring-boot-configuration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-configuration</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 注解 @ConfigurationProperties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 在配置文件
application.yml
中添加以下内容。
config:
data:
- A
- B
- C
- A
- 配置
Array
,创建配置类。
package tutorial.spring.boot.configuration.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("config")
public class Config {
private String[] data;
public String[] getData() {
return data;
}
public void setData(String[] data) {
this.data = data;
}
}
- 单元测试
package tutorial.spring.boot.configuration.config;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigTest {
@Autowired
private Config config;
@Test
public void testArray() {
String[] expected = new String[]{"A", "B", "C", "A"};
String[] actual = config.getData();
Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
Assert.assertEquals(expected[i], actual[i]);
}
}
}
测试结果略。
- 配置
List
不需要修改配置文件application.yml
,修改配置类Config
代码。
package tutorial.spring.boot.configuration.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties("config")
public class Config {
private List<String> data;
public List<String> getData() {
return data;
}
public void setData(List<String> data) {
this.data = data;
}
}
- 单元测试
@Test
public void testList() {
List<String> expected = new ArrayList<>();
Collections.addAll(expected, "A", "B", "C", "A");
List<String> actual = config.getData();
Assert.assertEquals(expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
Assert.assertEquals(expected.get(i), actual.get(i));
}
}
- 配置
Set
需格外小心,因为Set
中元素是无序且不重复的,所以按照以上配置文件内容生成的Set
中实际只包含 3 个元素,修改配置类Config
代码。
package tutorial.spring.boot.configuration.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Set;
@Component
@ConfigurationProperties("config")
public class Config {
private Set<String> data;
public Set<String> getData() {
return data;
}
public void setData(Set<String> data) {
this.data = data;
}
}
- 单元测试
@Test
public void testList() {
Assert.assertEquals(3, config.getData().size());
Set<String> expected = new HashSet<>(3);
Collections.addAll(expected, "A", "B", "C");
Set<String> actual = config.getData();
expected.forEach(i -> Assert.assertTrue(actual.contains(i)));
}
网友评论