带有StepScope Annotation的PoiItemReader不读取Excel文件

时间:2018-01-04 09:44:21

标签: java excel spring-boot spring-batch

我想将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 [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]

我有来自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());
      }
  }

}

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

  }
}

设置StepScope Annotation对于访问JobParameters是必需的,我没有将参数传递给FlatFileItemReader的问题。我认为问题是PoiItemReader。

如何通过PoiItemReader从Spring Batch的JobParameters中获取filePath来读取excelFile?

1 个答案:

答案 0 :(得分:1)

@Step范围不适用于通用读者,即ItemReader<StudentDTO>您需要将其更改为PoiItemReader<StudentDTO>