多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Spring Batch `ItemProcessor`示例 > 原文: [https://howtodoinjava.com/spring-batch/spring-batch-itemprocessor-example/](https://howtodoinjava.com/spring-batch/spring-batch-itemprocessor-example/) 在读取输入并将其传递给写入器以写入文件/数据库之前,学习使用`ItemProcessor`添加业务逻辑。 请注意,虽然返回的数据类型可能不同于输入提供的数据类型,但这不是必需的。 > 从`ItemProcessor`返回`null`表示不应继续处理该项目。 ## 如何编写`ItemProcessor` 下面给出的`ItemProcessor`实现执行以下任务: * 验证是否设置了`'id'`字段。 * 验证`'id'`字段是否可解析为整数。 * 验证`'id'`字段是否为大于零的正整数。 * 如果验证失败,则返回`null`,表示不处理记录。 * 如果验证成功,则按原样返回`Employee`对象。 `ValidationProcessor.java` ```java import org.springframework.batch.item.ItemProcessor; import com.howtodoinjava.demo.model.Employee; public class ValidationProcessor implements ItemProcessor<Employee,Employee> { public Employee process(Employee employee) throws Exception { if (employee.getId() == null){ System.out.println("Missing employee id : " + employee.getId()); return null; } try { if(Integer.valueOf(employee.getId()) <= 0) { System.out.println("Invalid employee id : " + employee.getId()); return null; } } catch (NumberFormatException e) { System.out.println("Invalid employee id : " + employee.getId()); return null; } return employee; } } ``` ## 如何使用`ItemProcessor` 在步骤中设置 Tasklet 的过程中,应使用`SimpleStepBuilder.processor()`设置处理器实例。 `BatchConfig.java` ```java package com.howtodoinjava.demo.config; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.LineMapper; import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import com.howtodoinjava.demo.model.Employee; @Configuration @EnableBatchProcessing public class BatchConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Value("/input/inputData.csv") private Resource inputResource; @Bean public Job readCSVFilesJob() { return jobBuilderFactory .get("readCSVFilesJob") .incrementer(new RunIdIncrementer()) .start(step1()) .build(); } @Bean public Step step1() { return stepBuilderFactory .get("step1") .<Employee, Employee>chunk(1) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @Bean public ItemProcessor<Employee, Employee> processor() { return new ValidationProcessor(); } @Bean public FlatFileItemReader<Employee> reader() { FlatFileItemReader<Employee> itemReader = new FlatFileItemReader<Employee>(); itemReader.setLineMapper(lineMapper()); itemReader.setLinesToSkip(1); itemReader.setResource(inputResource); return itemReader; } @Bean public LineMapper<Employee> lineMapper() { DefaultLineMapper<Employee> lineMapper = new DefaultLineMapper<Employee>(); DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames(new String[] { "id", "firstName", "lastName" }); lineTokenizer.setIncludedFields(new int[] { 0, 1, 2 }); BeanWrapperFieldSetMapper<Employee> fieldSetMapper = new BeanWrapperFieldSetMapper<Employee>(); fieldSetMapper.setTargetType(Employee.class); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(fieldSetMapper); return lineMapper; } @Bean public ConsoleItemWriter<Employee> writer() { return new ConsoleItemWriter<Employee>(); } } ``` ## `ItemProcessor`演示 我正在使用上述配置处理此 CSV。 `inputData.csv` ```java id,firstName,lastName 1,Lokesh,Gupta 2,Amit,Mishra 3,Pankaj,Kumar abc,David,Miller 4,David,Walsh ``` 开始作业并查看控制台。 `Console` ```java 2018-07-11 14:59:00 INFO - Job: [SimpleJob: [name=readCSVFilesJob]] launched with the following parameters: [{JobID=1531301340005}] 2018-07-11 14:59:00 INFO - Executing step: [step1] Employee [id=1, firstName=Lokesh, lastName=Gupta] Employee [id=2, firstName=Amit, lastName=Mishra] Employee [id=3, firstName=Pankaj, lastName=Kumar] Invalid employee id : abc Employee [id=4, firstName=David, lastName=Walsh] 2018-07-11 14:59:00 INFO - Job: [SimpleJob: [name=readCSVFilesJob]] completed with the following parameters: [{JobID=1531301340005}] and the following status: [COMPLETED] ``` 将我的问题放在评论部分。 学习愉快! 参考: [`ItemProcessor` Java 文档](https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/item/ItemProcessor.html)