PoiItemReader with StepScope Annotation do not rea

2020-04-12 07:32发布

I want to pass a JobParameter in Spring Batch to my PoiItemReader, to locate the Excelfilepath. So I have to use the 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;
}

The Job launched without exceptions, but the reader do not read the 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]  

If I remove the Annotation @StepScope and give the path directly to the ItemReader, the PoiItemReader read the ExcelFile.

Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{filePath=files/sample-data.xlsx, time=1515055832722}]  
Executing step: [step1]
StudentDTO [emailAddress=tony.tester@gmail.com , name=Tony Tester, purchasedPackage=master]  
StudentDTO [emailAddress=nick.newbie@gmail.com, name=Nick Newbie , purchasedPackage=starter]  
StudentDTO [emailAddress=ian.intermediate@gmail.com, 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]

I have the PoiItemReader from https://github.com/mdeinum/spring-batch-extensions
The Code is from the tutorial: 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());
      }
  }

}

Application.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());
      }

  }
}

Setting The StepScope Annotation is mandatory for accessing the JobParameters and I had no issues to pass Parameters to a FlatFileItemReader. I think the problem is the PoiItemReader.

How can I read an excelFile with the PoiItemReader getting the filePath from the JobParameters in Spring Batch?

1条回答
家丑人穷心不美
2楼-- · 2020-04-12 07:44

@Step Scope will not work with generic Reader i.e ItemReader<StudentDTO> You need to change it to PoiItemReader<StudentDTO>

查看更多
登录 后发表回答