目前,我为五个不同的类实现了此方法,为五个不同的文本文件配置FlatFileReader并加载到DB表中。
我想知道有没有办法在公共的地方实现这种方法,以便我的每个文件都会调用此方法来配置我的阅读器以读取其相应的文件。
每个文件都会发送不同的实体类来加载他们的文件。 如主
实体、子实体 1、子实体 2、子实体 3、子实体 4。private void flatFileItemReaderConfig(FlatFileItemReader<PrimaryEntity> flatFileReader,
String fileName, String incomingHeader)
{
// set delimiter as '|'
DefaultLineMapper<PrimaryEntity> defaultLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer("|");
delimitedLineTokenizer.setNames(convertHeaderToEntityAttrib(fileName, incomingHeader));
defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
// convert each record into primaryentity
BeanWrapperFieldSetMapper<PrimaryEntity> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
beanWrapperFieldSetMapper.setTargetType(PrimaryEntity.class);
beanWrapperFieldSetMapper.setCustomEditors(getCustomFormatters());
defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);
flatFileReader.setLineMapper(defaultLineMapper);
}
向方法添加泛型
private <T> void flatFileItemReaderConfig(FlatFileItemReader<T> flatFileReader, String fileName, String incomingHeader, Class<T> clazz) {
// set delimiter as '|'
DefaultLineMapper<T> defaultLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer("|");
delimitedLineTokenizer.setNames(convertHeaderToEntityAttrib(fileName, incomingHeader));
defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
// convert each record into primary entity
BeanWrapperFieldSetMapper<T> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
beanWrapperFieldSetMapper.setTargetType(clazz);
beanWrapperFieldSetMapper.setCustomEditors(getCustomFormatters());
defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);
flatFileReader.setLineMapper(defaultLineMapper);
}
如果要将用法限制为仅列出的实体,请创建一个由所有具体实体(PrimaryEntity, ChildEntity1, ChildEntity2, ChildEntity3, ChildEntity4
(实现的通用接口实体,并将签名更改为
private <T extends Entity> void flatFileItemReaderConfig(...)