我想将Spring Batch中的JobParameter传递给我的PoiItemReader,以找到Excelfilepath。所以我必须使用Annotation @StepScope
@StepScope
@Bean
ItemReader<StudentDTO> excelStudentReader( @Value("#{jobParameters[filePath]}") String filePath) {
PoiItemReader<StudentDTO> reader = new PoiItemReader<>();
reader.setResource(new ClassPathResource(filePath));
reader.setRowMapper(new StudentExcelRowMapper());
return reader;
}
作业无例外地启动,但是读者不阅读Excelfile。
Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{filePath=files/sample-data.xlsx, time=1515056077325}]
Executing step: [step1]
Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{filePath=files/sample-data.xlsx, time=1515056077325}] and the following status: [COMPLETED]
如果删除Annotation
@StepScope
并将路径直接提供给ItemReader,则PoiItemReader会读取ExcelFile。Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{filePath=files/sample-data.xlsx, time=1515055832722}]
Executing step: [step1]
StudentDTO [[email protected] , name=Tony Tester, purchasedPackage=master]
StudentDTO [[email protected], name=Nick Newbie , purchasedPackage=starter]
StudentDTO [[email protected], name=Ian Intermediate, purchasedPackage=intermediate]
Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{filePath=files/sample-data.xlsx, time=1515055966700}] and the following status: [COMPLETED]
我有来自https://github.com/mdeinum/spring-batch-extensions的PoiItemReader
该代码来自教程:https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/
BatchConfiguration.java
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@StepScope
@Bean
ItemReader<StudentDTO> excelStudentReader( @Value("#
{jobParameters[filePath]}") String filePath) {
PoiItemReader<StudentDTO> reader = new PoiItemReader<>();
reader.setResource(new ClassPathResource(filePath));
reader.setLinesToSkip(1);
reader.setRowMapper(new StudentExcelRowMapper());
return reader;
}
public CustomWriter writer() {
return new CustomWriter();
}
@Bean
public Job importUserJob() {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<StudentDTO, StudentDTO> chunk(10)
.reader(excelStudentReader(null))
.writer(writer())
.build();
}
}
CustomWriter.java
public class CustomWriter implements ItemWriter<StudentDTO> {
public void write(List<? extends StudentDTO> arg0) throws Exception {
for (StudentDTO s : arg0){
System.out.println(s.toString());
}
}
}
应用程序
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
try {
JobParameters param = new JobParametersBuilder().addString("filePath", "files/sample-data.xlsx")
.addLong("time", System.currentTimeMillis()).toJobParameters();
ApplicationContext context = new AnnotationConfigApplicationContext(BatchConfiguration.class);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean(Job.class);
jobLauncher.run(job, param);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
设置StepScope注释对于访问JobParameters是必需的,并且我没有问题可以将参数传递给FlatFileItemReader。我认为问题是PoiItemReader。
我如何使用PoiItemReader读取excelFile并从Spring Batch的JobParameters获取filePath?
最佳答案
@Step
范围不适用于通用阅读器,即ItemReader<StudentDTO>
您需要将其更改为PoiItemReader<StudentDTO>