title: creating custom ItemReader with defaultLineMapper using spring annotations
body: <p>I am trying to make a annotation based ItemReader in spring-batch. I was able to make the desired ItemReader in my xml but annotation based is not at all working giving the following error: </p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'runScheduler': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.Job com.techavalanche.batch.RunScheduler.job; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportJob': Cannot create inner bean '(inner bean)#3f9bb1' of type [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean] while setting bean property 'flow'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3f9bb1': Cannot create inner bean '(inner bean)#17e6f05' of type [org.springframework.batch.core.job.flow.support.StateTransition] while setting bean property 'stateTransitions' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#17e6f05': Cannot create inner bean '(inner bean)#66da50' of type [org.springframework.batch.core.job.flow.support.state.StepState] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#66da50': Cannot resolve reference to bean 'step1' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'customItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customItemReader': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.batch.item.file.FlatFileItemReader com.techavalanche.reader.CustomReader.itemReader; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getFlatFileItemReader' defined in class path resource [com/techavalanche/reader/CustomReader.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: LineMapper is required
  </code></pre>
  
  <p>and it is caused by : </p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.Job com.techavalanche.batch.RunScheduler.job; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportJob': Cannot create inner bean '(inner bean)#3f9bb1' of type [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean] while setting bean property 'flow'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3f9bb1': Cannot create inner bean '(inner bean)#17e6f05' of type [org.springframework.batch.core.job.flow.support.StateTransition] while setting bean property 'stateTransitions' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#17e6f05': Cannot create inner bean '(inner bean)#66da50' of type [org.springframework.batch.core.job.flow.support.state.StepState] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#66da50': Cannot resolve reference to bean 'step1' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'customItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customItemReader': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.batch.item.file.FlatFileItemReader com.techavalanche.reader.CustomReader.itemReader; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getFlatFileItemReader' defined in class path resource [com/techavalanche/reader/CustomReader.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: LineMapper is required
  </code></pre>
  
  <p>My CustomReader is </p>
  
  <pre><code>package com.techavalanche.reader;
  
  @Component("customReader")
  public class CustomReader implements ItemReader&lt;Report&gt; {
  
  
      @Autowired
      public FlatFileItemReader&lt;Report&gt; itemReader;
  
      @Bean 
      public FlatFileItemReader getFlatFileItemReader(){
  
          return new FlatFileItemReader();
      }
  
  
      @Bean
      public DefaultLineMapper getDefaultLineMapper(){
  
          return new DefaultLineMapper();
      }
  
      @Autowired
      public DefaultLineMapper&lt;Report&gt; lineMapper;
  
  
  
      @Bean
      public FixedLengthTokenizer getLinetokenizer(){     
          return new FixedLengthTokenizer();
      }
      @Autowired
      public FixedLengthTokenizer linetokenizer;
  
      @Bean
      public BeanWrapperFieldSetMapper getBeanWrapperFieldSetMapper() {
  
          return new BeanWrapperFieldSetMapper();
      }
  
  
      @Autowired  
      public BeanWrapperFieldSetMapper&lt;Report&gt; fieldSetMapper;
  
      public Resource res=new FileSystemResource("D:\\testresource\\report.csv");
  
  
  
      String[] names= {"date","impressions","clicks","earning"};
  
  
      @Override
      public Report read() throws Exception, UnexpectedInputException, ParseException,
      NonTransientResourceException {
  
  
          for(String name:names){         
              System.out.println(name);
          }
  
          linetokenizer.setNames(new String[] {"date","impressions","clicks","earning"}); 
  
          Range range1=new Range(1,12);
          Range range2=new Range(13,15);
          Range range3=new Range(16,20);
          Range range4=new Range(21,28);
          Range[] ranges={range1,range2,range3,range4};
  
          linetokenizer.setColumns(ranges);
          lineMapper.setLineTokenizer(linetokenizer);     
  
  
          fieldSetMapper.setPrototypeBeanName("report");      
          lineMapper.setFieldSetMapper(fieldSetMapper);
  
          itemReader.setResource(res);
          itemReader.setLineMapper(lineMapper);   
  
  
          itemReader.open(new ExecutionContext());
          Report report= itemReader.read();       
  
          return report;
  
      }
  }
  </code></pre>
  
  <p>My Xml Configuration looks like this: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:batch="http://www.springframework.org/schema/batch"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/batch
      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-3.2.xsd
      "&gt;
       &lt;context:component-scan base-package="com.techavalanche" /&gt;
      &lt;bean id="jobRepository"
          class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"&gt;
          &lt;property name="transactionManager" ref="transactionManager" /&gt;
      &lt;/bean&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /&gt;
  
      &lt;bean id="jobLauncher"
          class="org.springframework.batch.core.launch.support.SimpleJobLauncher"&gt;
          &lt;property name="jobRepository" ref="jobRepository" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="report" class="com.techavalanche.batch.Report" scope="prototype" /&gt;
  
      &lt;bean id="customWriter" class="com.techavalanche.writer.CustomWriter" /&gt;
  
  
      &lt;batch:job id="reportJob"&gt;
          &lt;batch:step id="step1"&gt;
              &lt;batch:tasklet&gt;
                  &lt;batch:chunk reader="customItemReader" writer="customWriter"
                      commit-interval="2"&gt;
                  &lt;/batch:chunk&gt;
              &lt;/batch:tasklet&gt;
          &lt;/batch:step&gt;
      &lt;/batch:job&gt;
  
      &lt;bean id="multiResourceReader"
          class=" org.springframework.batch.item.file.MultiResourceItemReader"&gt;
          &lt;property name="resources" value="file:D:\testresource\report.csv" /&gt;
          &lt;property name="delegate" ref="cvsFileItemReader" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="customItemReader" class="com.techavalanche.reader.CustomReader" /&gt;
      &lt;bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
          &lt;!-- Read a csv file --&gt;
          &lt;!-- &lt;property name="resource" ref="resource" /&gt; --&gt;
  
          &lt;property name="lineMapper"&gt;
              &lt;bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
                  &lt;!-- split it --&gt;
                  &lt;property name="lineTokenizer"&gt;
                      &lt;bean id="fixedLengthLineTokenizer"
                          class="org.springframework.batch.item.file.transform.FixedLengthTokenizer"&gt;
                          &lt;property name="names" value="date,impressions,clicks,earning" /&gt;
                          &lt;property name="columns" value="1-12,13-15,16-20,21-28" /&gt;
                      &lt;/bean&gt;
                  &lt;/property&gt;
                  &lt;property name="fieldSetMapper"&gt;
                      &lt;!-- return back to reader, rather than a mapped object. --&gt;
                      &lt;!-- &lt;bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" 
                          /&gt; --&gt;
                      &lt;!-- map to an object --&gt;
                      &lt;bean
                          class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"&gt;
                          &lt;property name="prototypeBeanName" value="report" /&gt;
                      &lt;/bean&gt;
                  &lt;/property&gt;
  
              &lt;/bean&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>