找到an accepted answer from a Spring Batch dev here以及下面的随附JavaConfig代码之后,我仍然对如何使用stepScope()感到困惑。我试图将multiResourceItemReader的作用域设置为以下范围,但是仅将stepScope()的bean定义添加到文件的顶部或底部会导致此错误:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readFiles' defined in class path resource [com/onlinephotosubmission/csvImporter/service/BatchJobService.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [java.lang.Object]: Factory method 'readFiles' threw exception;
nested exception is java.lang.NullPointerException:
Cannot invoke method get() on null object
我所知道的是stepScope()必须位于@Configuration文件中,除此之外,对于需要完成的工作,我感到很困惑。
BatchJobService.groovy
@Configuration
@EnableBatchProcessing
class BatchJobService {
@Autowired
JobBuilderFactory jobBuilderFactory
@Autowired
StepBuilderFactory stepBuilderFactory
@Autowired
JobLauncher jobLauncher
@Autowired
AfterJobListener afterJobListener
// Set values from properties file
@Value('${input.directory:file:inputs/*.csv}')
Resource[] resources
@Value('${report.directory:output}')
String reportsDir
@Value('${completed.directory:completed}')
String completedDir
@Value('${report.name.prepend:people}')
String prependName
@Value('${timestamp.format:dd_MM_yyyy_HH_mm_ss}')
String timestampFormat
// End set properties
@Bean
StepScope stepScope() {
final StepScope stepScope = new StepScope()
stepScope.setAutoProxy(true)
return stepScope
}
@Bean
Job readFiles() {
return jobBuilderFactory
.get("readFiles")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.listener(afterJobListener)
.build()
}
@Bean
Step step1() {
return stepBuilderFactory
.get("step1")
//NOTE: may need to adjust chunk size larger (say 1000 to take all transacions at once)
// or smaller (say 1 to take each transaction individually).
// Bigger is usually better, though.
.<Person, Person>chunk(1000)
.reader(multiResourceItemReader())
.processor(modifier())
.writer(writer())
.build()
}
@Bean
MultiResourceItemReader<Person> multiResourceItemReader() {
MultiResourceItemReader<Person> resourceItemReader = new MultiResourceItemReader<Person>()
resourceItemReader.setResources(resources)
resourceItemReader.setDelegate(reader())
return resourceItemReader
}
@Bean
FlatFileItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>()
reader.setLinesToSkip(1) //skips header line
reader.setLineMapper(new DefaultLineMapper()
{{
setLineTokenizer(new DelimitedLineTokenizer(",")
{{
setNames(["email", "identifier"] as String[])
}})
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() // BeanWrapperFieldSetMapper maps the line token values to a POJO directly by name
{{
setTargetType(Person.class)
}})
}})
return reader
}
@Bean
PersonItemProcessor modifier(){
return new PersonItemProcessor()
}
@Bean
FlatFileItemWriter<Person> writer() {
FlatFileItemWriter<Person> writer = new FlatFileItemWriter<>()
writer.setAppendAllowed(true)
writer.setResource(new FileSystemResource(reportsDir + "/" + prependName + getTime() + ".csv"))
writer.setLineAggregator(new DelimitedLineAggregator<Person>()
{{
setDelimiter(",")
setFieldExtractor(new BeanWrapperFieldExtractor<Person>()
{{
setNames(["status", "email", "identifier"] as String[])
}})
}})
return writer
}
}
答案 0 :(得分:1)
@EnableBatchProcessing
自动导入StepScope
,因此您无需在应用程序上下文中将其声明为bean。当XML和Java Config混合使用时,您链接到的问题就会发生。就您的情况而言,我仅看到Java Config,因此该问题不会发生。
我正在尝试将multiResourceItemReader的作用域设置在下面
我所知道的是stepScope()必须位于@Configuration文件中,除此之外,对于需要完成的工作,我感到很困惑。
仅声明步骤范围还不够,您需要在bean定义上添加@StepScope
批注。
您可以在以下参考文档中找到有关StepScope
的更多详细信息:https://docs.spring.io/spring-batch/4.0.x/reference/html/step.html#step-scope