我想将 Spring Batch 中的 JobParameter 传递给我的 PoiItemReader,以找到 Excelfilepath。所以我必须使用注释@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]
如果我删除注释@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] /cdn-cgi/l/email-protection , name=Tony Tester, purchasedPackage=master]
StudentDTO [[email protected] /cdn-cgi/l/email-protection, name=Nick Newbie , purchasedPackage=starter]
StudentDTO [[email protected] /cdn-cgi/l/email-protection, 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]
我有 PoiItemReader 来自https://github.com/mdeinum/spring-batch-extensions https://github.com/mdeinum/spring-batch-extensions
代码来自教程:https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/ https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/
批量配置.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());
}
}
}
应用程序.java
@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 从 Spring Batch 中的 JobParameters 获取 filePath 来读取 excelFile?