美文网首页
Spring Boot 配置数组、Map、随机值及转义字符

Spring Boot 配置数组、Map、随机值及转义字符

作者: 又语 | 来源:发表于2019-07-24 08:33 被阅读0次

本文介绍 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

  1. 创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程

  2. 生成的 pom 文件如下,添加 spring-boot-configuration-processorjunit 依赖。

<?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>
  1. 在配置文件 application.yml 中添加以下内容。
config:
  data:
    - A
    - B
    - C
    - A
  1. 配置 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;
    }
}
  1. 单元测试
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]);
        }
    }
}

测试结果略。

  1. 配置 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;
    }
}
  1. 单元测试
@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));
    }
}
  1. 配置 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;
    }
}
  1. 单元测试
@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)));
}

配置 Map


配置随机值


配置转义字符

相关文章

网友评论

      本文标题:Spring Boot 配置数组、Map、随机值及转义字符

      本文链接:https://www.haomeiwen.com/subject/wvktrctx.html