Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有StepScope批注的PoiItemReader不读取Excel文件_Java_Excel_Spring Boot_Spring Batch - Fatal编程技术网

Java 带有StepScope批注的PoiItemReader不读取Excel文件

Java 带有StepScope批注的PoiItemReader不读取Excel文件,java,excel,spring-boot,spring-batch,Java,Excel,Spring Boot,Spring Batch,我想将Spring批处理中的JobParameter传递给我的PoItemReader,以定位Excelfilepath。因此,我必须使用注释@StepScope @StepScope @Bean ItemReader<StudentDTO> excelStudentReader( @Value("#{jobParameters[filePath]}") String filePath) { PoiItemReader<StudentDTO> reader = n

我想将Spring批处理中的JobParameter传递给我的PoItemReader,以定位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;
}
如果我删除注释
@StepScope
,并将路径直接提供给ItemReader,POITEMReader将读取EXCEL文件

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]
我有来自
代码来自教程:

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();
  }
}
设置StepScope注释对于访问JobParameters是必需的,我没有问题将参数传递给FlatFileItemReader。我认为问题出在读者身上


如何在POITEMReader从Spring批处理中的JobParameters获取文件路径的情况下读取Excel文件?

@Step
作用域不适用于通用读取器,即
ItemReader
您需要将其更改为
POITEMReader

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

  }
}