gpt4 book ai didi

java - 带有 StepScope 注释的 PoiItemReader 不读取 Excel 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:24 29 4
gpt4 key购买 nike

我想将 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;
}

作业无一异常(exception)地启动,但读者不读取 Excel 文件。

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/

批量配置.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();
}
}

自定义编写器.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 获取文件路径来读取 excelFile?

最佳答案

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

关于java - 带有 StepScope 注释的 PoiItemReader 不读取 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48092526/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com