我有一個需求,我需要根據剩餘的處理文件來處理文件,我在其中獲取文件的名稱,我將它添加到作業參數並在創建bean時使用它。Spring批處理步驟作用域如何工作
我正在創建步驟作用域Bean(讀者,作家)和使用作業參數。我在新線程中啓動作業,因爲我使用異步任務exceutor來啓動作業,我的問題是bean當我們定義@StepScope
jobParametersBuilder.addString("fileName", request.getFileName());
jobExecution = jobLauncher.run(job, jobParametersBuilder.toJobParameters());
@Bean
public JobLauncher jobLauncher() {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository());
jobLauncher.setTaskExecutor(asyncTaskExecutor());
return jobLauncher;
}
@Bean
@StepScope
public ItemWriter<Object> writer(@Value ("#{jobParameters['fileName']}"String fileName) {
JdbcBatchItemWriter<Object> writer = new JdbcBatchItemWriter<>();
writer.setItemSqlParameterSourceProvider(
new BeanPropertyItemSqlParameterSourceProvider<Object>());
writer.setSql(queryCollection.getquery());
writer.setDataSource(dataSource(fileName));
return writer;
}