【发布时间】: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