当我尝试在ItemReader中自动装配任何bean时,我得到了一个NPE。有人可以帮我解决我出错的地方吗?谢谢!
要求是我想在ItemReader中注入一个DAO来从多个源获取数据并创建一个对象。
这是我的Config类 -
@Configuration
@EnableBatchProcessing
@EnableScheduling
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class ReportMetadataConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job fetchReportMetadataJob;
@Bean
public Job fetchReportMetadataJob() {
return jobBuilderFactory.get("fetchReportMetadataJob")
.incrementer(new RunIdIncrementer()).listener(listener())
.flow(reportWriterStep()).end().build();
}
@Bean
public Step reportWriterStep() {
return stepBuilderFactory.get("reportWriterStep").<ReportMetadataDTO, ReportMetadataDTO> chunk(1)
.reader(compositeItemReader())
.processor(new ReportMetadataProcessor())
.writer(reportMetadataWriter()).build();
}
public ItemReader<ReportMetadataDTO> compositeItemReader() {
return new CompositeItemReader();
}
public ItemWriter<ReportMetadataDTO> reportMetadataWriter() {
return new ReportMetadataWriter();
}
@Bean
public JobExecutionListener listener() {
return new ReportMetadataCompletionListener();
}
@Bean
public ResourcelessTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
}
这是我创建的复合ItemReader -
@Component
public class CompositeItemReader implements ItemReader<ReportMetadataDTO> {
private ItemReader<String> recordReader;
@Autowired
private ReportMetadataService reportMetadataService;
@Override
public ReportMetadataDTO read() throws Exception, UnexpectedInputException, ParseException,
NonTransientResourceException {
reportMetadataService.getReportMetada();
/*if(recordReader == null) {
recordReader = new IteratorItemReader<String>(getRecords());
}*/
ReportMetadataDTO data = new ReportMetadataDTO();
//return recordReader.read();
return data;
}
}
这是我想要在ItemReader中调用Autowired的服务。
@Service
public class ReportMetadataServiceImpl implements ReportMetadataService {
@Autowired
private ReportMetadataDao reportMetadataDao;
@Override
public void getReportMetada() {
reportMetadataDao.getReportDetails();
}
}
编辑 -
堆栈追踪 -
java.lang.NullPointerException: null
at com.test.batch.jobs.ReportMetadata.step.CompositeItemReader.read(CompositeItemReader.java:40)
at com.test.batch.jobs.ReportMetadata.step.CompositeItemReader.read(CompositeItemReader.java:1)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy74.run(Unknown Source)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.test.CustomReportWriterApplication.main(CustomReportWriterApplication.java:12)
答案 0 :(得分:2)
您没有正确连接bean。您希望在类上使用@Component
并让Spring创建它或在工厂方法上添加@Bean
并创建它。在这种情况下,你有@Component
所以你得到一个Spring知道的实例,但那不是你注入ItemReader
的实例。你注入ItemReader
的那个是一个没有自动装配的原因(因为Spring不知道它)。我建议您从@Component
删除CompositeItemReader
并将@Bean
添加到public ItemReader<ReportMetadataDTO> compositeItemReader()
。