title: Spring Boot + Batch : Injected/Autowired beans are null in ItemReader
body: <p>I get a NPE when I try to Autowire any bean in the ItemReader. Can somebody please help me with where I'm going wrong? Thanks!</p>
  
  <p>The requirement is I'd like to inject a DAO in the ItemReader to fetch data from multiple sources and create a single object.</p>
  
  <p>This is the Config Class that I have -</p>
  
  <pre><code>    @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").&lt;ReportMetadataDTO, ReportMetadataDTO&gt; chunk(1)
                  .reader(compositeItemReader())
                  .processor(new ReportMetadataProcessor())
                  .writer(reportMetadataWriter()).build();
      }
  
      public ItemReader&lt;ReportMetadataDTO&gt; compositeItemReader() {
          return new CompositeItemReader();
      }
  
      public ItemWriter&lt;ReportMetadataDTO&gt; reportMetadataWriter() {
          return new ReportMetadataWriter();
      }
  
      @Bean
      public JobExecutionListener listener() {
          return new ReportMetadataCompletionListener();
      }
  
      @Bean
      public ResourcelessTransactionManager transactionManager() {
          return new ResourcelessTransactionManager();
      }
  
  
  }
  </code></pre>
  
  <p>This is the composite ItemReader that i've created -</p>
  
  <pre><code>    @Component
  public class CompositeItemReader implements ItemReader&lt;ReportMetadataDTO&gt; {
  
      private ItemReader&lt;String&gt; recordReader;
  
      @Autowired
      private ReportMetadataService reportMetadataService;
  
      @Override
      public ReportMetadataDTO read() throws Exception, UnexpectedInputException, ParseException,
              NonTransientResourceException {
  
          reportMetadataService.getReportMetada();
          /*if(recordReader == null) {
              recordReader = new IteratorItemReader&lt;String&gt;(getRecords());
          }*/
          ReportMetadataDTO data = new ReportMetadataDTO();
          //return recordReader.read();
          return data;
      }
  
  }
  </code></pre>
  
  <p>This is the service I'd like to call which is Autowired in the ItemReader.</p>
  
  <pre><code>    @Service
  public class ReportMetadataServiceImpl implements ReportMetadataService {
  
      @Autowired
      private ReportMetadataDao reportMetadataDao;
  
      @Override
      public void getReportMetada() {
          reportMetadataDao.getReportDetails();
      }
  
  }
  </code></pre>
  
  <p>Edit -</p>
  
  <p>Stack Trace -</p>
  
  <pre><code>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)
  </code></pre>