【问题标题】:Use of Factory Pattern in Spring Batch FileHeaderFieldSetMapperSpring Batch FileHeaderFieldSetMapper中工厂模式的使用
【发布时间】:2018-11-04 19:37:06
【问题描述】:

我正在设置一个处理多记录文件的 Spring Batch 作业。其中一条记录具有以下结构。

Ind,RecCode,AccNum,RecTypeDesc,EffectiveDate,CreditAmount,DebitAmount,BatchNumber,NumberOfTrasactions 
5, 20, 100, prl, 12084, as0, 0, 3, 1

根据 RecordCode 字段中的值,它将是付款记录或冲销记录。

我正在设置 PatternMatchingCompositeLineMapper 来读取文件中的记录类型。我创建了以下 2 个 FieldSetMapper 实现类

@Component("paymentBatchFieldSetMapper")
public class  PaymentBatchFieldSetMapper implements FieldSetMapper<PaymentBatch> {

    @Override
    public PaymentBatch mapFieldSet(FieldSet fieldSet) throws BindException {
        // TODO Auto-generated method stub
        return null;
    }
}

第二个是,

@Component("reversalBatchFieldSetMapper")
public class  ReversalBatchFieldSetMapper implements FieldSetMapper<PaymentBatch> {

    @Override
    public PaymentBatch mapFieldSet(FieldSet fieldSet) throws BindException {
        // TODO Auto-generated method stub
        return null;
    }
}

我创建了一个工厂类,如下所示返回以上 2 个中的任何一个

@Component("achBatchFieldSetMapperFactory")
public class ACHBatchFieldSetMapperFactory {

    @Autowired
    private ApplicationContext applicationContext;

    public FieldSetMapper<? extends AbstractACHBatch> getFieldSetMapper(RecordTypeDescription recordTypeDescription) {
        FieldSetMapper<? extends AbstractACHBatch> fieldSetMapper = null;
        switch(recordTypeDescription) {
        case PAYMENT:
            fieldSetMapper = applicationContext.getBean(PaymentBatchFieldSetMapper.class);
            break;
        case REVERSAL:
            fieldSetMapper = applicationContext.getBean(ReversalBatchFieldSetMapper.class);
            break;
        }
        return fieldSetMapper;
    }
}

下面是要注入到 PatternMatchingCompositeLineMapper 中的地图对象的 bean 配置。

@Bean
public Map<String, LineTokenizer> fieldSetMapperMap(FileHeaderFieldSetMapper fileHeaderFieldSetMapper, PaymentBatchFieldSetMapper paymentBatchFieldSetMapper,
        ReversalBatchFieldSetMapper reversalBatchFieldSetMapper) {
    Map<String, FieldSetMapper> map = new HashMap<String, FieldSetMapper>();
    map.put("1*", fileHeaderFieldSetMapper);
    map.put("5*", ?????);                   
}

是否有可能我可以使用工厂根据文件中的记录返回所需的 FieldSet 映射器?

【问题讨论】:

    标签: spring-batch factory-pattern


    【解决方案1】:

    项目阅读器旨在一次返回一种类型的项目。假设我们设法根据RecordCode 字段将记录映射到PaymentBatchReversalBatch,这意味着映射发生后,ItemReader 将返回两种类型。这在设计上是不可能的。

    您可以做的是创建一个 POJO,它与输入文件中的字段具有一对一映射,然后使用 ClassifierCompositeItemProcessor 根据其类型处理每个项目(分类器将基于RecordTypeDescription 枚举在你的情况下)。

    注意:当然,在技术上可以忘记ItemReader&lt;T&gt;接口的泛型类型,让项目阅读器返回Objects,但这需要@987654330的丑陋用法@ 在您的处理器中确定记录类型。我强烈建议不要使用这种方法。

    【讨论】:

    • 非常感谢您的指导。我在使用分类器配置 ItemProcessor 时遇到了 1 个问题,对此我会提出另一个问题。
    • 您可以在此处找到ClassifierCompositeItemProcessor 的示例:github.com/spring-projects/spring-batch/blob/master/…。它们显示了PatternMatchingClassifierSubclassClassifier 的用法。在您的情况下,自定义分类器将获取对象(将一对一映射到您的文件)并根据其RecordTypeDescription 的值返回PaymentBatchReversalBatch 的项目处理器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多