java - 配置不同配置的 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 做类似的事情。
解决方案
推荐阅读
- ubuntu - 删除用户密码后无法登录
- java - 如何解决线程“AWT-EventQueue-0”中的此代码错误异常 java.lang.NoClassDefFoundError: simrskhanza/frmUtama
- qt - 如何在 clion 中运行 qt creator 演示代码?
- java - 在构建的jar java中使用相对文件路径访问文件
- java - 是否可以在一行代码中执行两种不同的“+=”和“-=”操作?
- graphql - 我应该如何将本地服务器与 graphQL 模式结合使用?
- java - 我无法让 ChartPanel 出现在现有的 jPanel 中。这和马蒂斯有关系吗?
- c# - Async 方法似乎没有异步运行 C#
- ios - 无法从数据库中检索评论 (FIREBASE/IOS)
- c# - 超出了内部响应标头大小限制