带 StepScope 注释的 PoiItemReader 不读取 Excel 文件 [英] PoiItemReader with StepScope Annotation do not read Excel file

查看:22
本文介绍了带 StepScope 注释的 PoiItemReader 不读取 Excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Spring Batch 中的 JobParameter 传递给我的 PoiItemReader,以定位 Excelfilepath.所以我必须使用注解 @StepScope

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

Job 无一例外地启动了,但读者没有读取 Excel 文件.

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]  

如果我删除 Annotation @StepScope 并直接将路径提供给 ItemReader,PoiItemReader 将读取 ExcelFile.

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]

我有来自 https://github.com/mdeinum/spring-batch-的 PoiItemReader-扩展
代码来自教程:https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/

批处理配置.java

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

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.

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.

如何使用 PoiItemReader 从 Spring Batch 中的 JobParameters 获取文件路径来读取 excelFile?

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

推荐答案

@Step Scope will not work with generic Reader ie ItemReader 你需要改变它PoiItemReader

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

这篇关于带 StepScope 注释的 PoiItemReader 不读取 Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆