首页 > 解决方案 > 配置不同配置的 Bean 列表

问题描述

我有一个用于许多不同公司的 Spring Boot 数据清理应用程序。我建立了一个清洁模块库,我们的团队可以根据需要为特定客户进行配置。我正在寻找扩展我已经构建的东西,但我无法让它工作。

总结这个问题,我想我正在尝试拥有一个每个配置不同的bean列表,其中可能有同一个类的多个实例。

这是所需配置的示例(使用 application.yml):

app.dataEnrichmentSteps:
  - name: Module1 #used to derive name of the Class that has the functionality
    filters:
      - name: FieldIsNotBlank #used to derive name of "filter" classname
        field: someRecordFieldName
      - name: FieldIsBlank
        field: someOtherRecordFieldName
    config:
      mod1cfg-a: someVal
      mod1cfg-b: someVal
  - name: Module2
    filters:
      - name: FieldIsNotBlank #used to derive name of "filter" classname
        field: someRecordFieldName
    config:
      mod2cfg-a: someVal
      mod2cfg-b: someVal
  - name: Module1 #this instance of Module1 has different config, and needs to run after the previous 2
    config:
      mod1cfg-a: someOtherVal
      mod2cfg-b: someOtherValAgain
  - name: Module3
    config:
      mod3cfg-a: hopefully you get the point

该应用程序需要足够灵活,以允许任何模块、任何顺序、任何次数。目前,在应用程序启动时,我有一个@Bean拉入步骤并构建服务实例(基于名称)并传入配置。这是 Spring 为我填充的 ​​Configuration 类(我不喜欢它,但我只能工作):

@Data
@Component
@ConfigurationProperties(prefix="app")
public class ModuleConfig {
    private List<EnrichmentConfig> dataEnrichmentSteps;

    @Data
    public static class EnrichmentConfig{
        //what kind of Enrichment are we doing?
        private String name;

        //how is the enrichment service configured?
        private HashMap<String, String> config; 

        //on which fields should the enrichment execute on?
        private List<HashMap<String,String>> filters; 
    }
}

我正在为这种配置的动态特性而苦苦挣扎。理想情况下,如果每个模块都可以为其配置定义一个 POJO 而不是我使用 Maps,我绝对会喜欢它,但是我遇到的所有内容都建议一个固定的配置结构,而不是我认为需要灵活的结构。

我熟悉@JsonTypeInfo并且@JsonSubTypes在使用 Jackson 并拥有一个可能具有一些动态结构化 JSON 的 JSON 有效负载时,尽管我似乎无法弄清楚如何为 Spring Configuration 做类似的事情。

标签: javaspringspring-bootconfiguration

解决方案


推荐阅读