# 系统配置

since v2.5.0 系统配置功能适用于需要线上配置调整的系统运行所需参数的场景,比如配置用于邮件发送的账号密码等

  • 简化动态配置,配套前端可修改配置
  • 支持多租户配置隔离,互不干扰

# 相关接口

# SystemConfigType

  • 用于定义配置枚举类
  • 示例:
// 实现 SystemConfigType 接口的相关方法
public enum EmailConfig implements SystemConfigType {
    /**
     * 服务器主机(默认值可自动读取配置文件,及用冒号分隔指定当配置文件中未配置时的默认值 )
     */
    host("主机", "${spring.mail.host:smtp.qq.com}", true) {
        /**
         * 定义配置选项
         */
        @Override
        public Set<String> options() {
            return new HashSet<String>(){{
                add("smtp.qq.com");
                add("smtp.136.com");
            }};
        }
    },
    /**
     * 用户名(默认值可自动读取配置文件, 配置文件中未配置时默认值为null)
     */
    username("用户名", "${spring.mail.username}", true),
    /**
     * 密码(直接指定默认值)
     */
    password("授权码", 123456, false);

    private final String propLabel;
    private final Object defaultValue;
    private final boolean required;

    EmailConfig(@NonNull String propLabel, Object defaultValue, boolean required) {
        this.propLabel = propLabel;
        this.defaultValue = defaultValue;
        this.required = required;
    }

    @Override
    public @NonNull String typeLabel() {
        return "邮箱配置";
    }

    @Override
    public @NonNull String propLabel() {
        return propLabel;
    }

    @Override
    public Object defaultValue() {
        return defaultValue;
    }

    @Override
    public boolean required() {
        return required;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# SystemConfigTest

某些场景下需要在线测试验证所配置参数是否正确,可对应实现配置参数测试方法。 不需要测试的配置参数使用场景,可忽略该项。

  • 用于提供给前端修改配置时测试使用
  • 示例:
/**
 * 测试数据
 */
@Data
public class EmailConfigTestData {
    /**
     * 接收者
     */
    @Email(message = "接收者邮箱不正确", regexp = "^[\\w-]+@[\\w-]+(\\.[\\w-]+)+$")
    @NotNull(message = "接收者不能为空")
    private String to;
    /**
     * 标题
     */
    @NotNull(message = "标题不能为空")
    private String title;
    /**
     * 内容
     */
    @NotNull(message = "内容不能为空")
    private String content;
}
// 实现 SystemConfigTest 的 test 方法;当只需要测试方法,不需要参数时,可不指定其泛型或用?代替
public enum EmailConfig implements SystemConfigType, SystemConfigTest<EmailConfigTestData> {
    
    // ... 

    /**
     * 测试方法
     * 
     * @param data 测试数据
     */
    @Override
    public void test(EmailConfigTestData data) {
        String errMsg = V.validateBeanErrMsg(data);
        if (errMsg != null) {
            throw new BusinessException(errMsg);
        }
        // 发送邮件...
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# SystemConfigInjection

配置类的声明方式:

  • 将配置类型注入spring上下文中,便于收集配置类型
  • 示例:
/**
 * 注入系统配置枚举类
 */
@Bean
public SystemConfigInjection systemConfig() {
    return () -> Arrays.asList(EmailConfig.class, ...);
}
1
2
3
4
5
6
7

# 使用说明

# 获取单个配置值

获取单个值时可通过枚举值.getValue()直接获取到最新配置值

String username = EmailConfig.username.getValue();
1

# 获取同类型多个配置值

获取同类型的多个值可以采用以下方式获取,避免多次查询

SystemConfigType.Values<EmailConfig> values = SystemConfigType.values(EmailConfig.class);
String username = values.get(EmailConfig.username);
String password = values.get(EmailConfig.password);
1
2
3