gpt4 book ai didi

spring-boot - Spring Boot + 批处理 : Injected/Autowired beans are null in ItemReader

转载 作者:行者123 更新时间:2023-12-03 19:37:15 25 4
gpt4 key购买 nike

当我尝试 Autowiring ItemReader 中的任何 bean 时,我得到一个 NPE。有人可以帮我解决我哪里出错了吗?谢谢!

要求是我想在 ItemReader 中注入(inject)一个 DAO 以从多个源获取数据并创建一个对象。

这是我拥有的配置类 -

    @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 中 Autowiring 。
    @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)

最佳答案

你没有正确连接你的bean。您想使用 @Component在你的类(class)上,让 Spring 创建或添加 @Bean在工厂方法上并创建它。在这种情况下,您有 @Component所以你得到了一个 Spring 知道的实例,但这不是你注入(inject)到 ItemReader 中的实例。 .你注入(inject)到你的ItemReader中的那个是一个普通的,不是 Autowiring 的(因为 Spring 不知道它)。我建议删除 @Component来自您的CompositeItemReader并添加 @Beanpublic ItemReader<ReportMetadataDTO> compositeItemReader() .

关于spring-boot - Spring Boot + 批处理 : Injected/Autowired beans are null in ItemReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828464/

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