Inject beans into spring-batch item reader
<p><strong>See below for 1 update</strong></p>

<p>I'm trying to inject a Service or DAO bean into my spring-batch ItemReader. However the injected reference is NULL. I tried this using @Autowire and explicit bean configuration in the XML file.</p>

<p>The job is being run from a spring-mvc application. This ItemReader requires DB access to load a list of ID's to process. The reader itereates through this list and the ItemWriter makes the necessary changes on the database for each ID using my existing Service and DAO Layers.</p>

<p>Here's my configuration (it is defined on a separate spring-batch.xml file included on the web.xml after the application context XML file.)</p>

<p>Also, in this version I'm trying to inject the JobService bean into the ItemReader using the XML configuration. </p>

<p>The general problem here is: how to inject other DAO's and/or Services into Item Readers and Writers? </p>

<p>
    </p>

<pre><code>&lt;beans:bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"&gt;
    &lt;beans:property name="dataSource" ref="dataSourceJNDI" /&gt;
    &lt;beans:property name="transactionManager" ref="transactionManager" /&gt;
    &lt;beans:property name="databaseType" value="mysql" /&gt;
&lt;/beans:bean&gt;

&lt;beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"&gt;
    &lt;beans:property name="jobRepository" ref="jobRepository" /&gt;
&lt;/beans:bean&gt;

&lt;beans:bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" /&gt;

&lt;beans:bean id="myItemReader" class="my.MyItemReader" scope="step"&gt;
    &lt;beans:property name="jobService"&gt;
        &lt;beans:bean class="my.JobService"&gt;&lt;/beans:bean&gt;
    &lt;/beans:property&gt;
&lt;/beans:bean&gt;

&lt;job id="assignLeadBatch" restartable="false" job-repository="jobRepository"&gt;
    &lt;step id="step1"&gt;
        &lt;tasklet transaction-manager="dbOpsTransactionManager"&gt;
            &lt;chunk reader="myItemReader" writer="myItemWriter" commit-interval="10" /&gt;
        &lt;/tasklet&gt;
    &lt;/step&gt;
&lt;/job&gt;

&lt;beans:bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor"&gt;
    &lt;beans:property name="jobRegistry" ref="jobRegistry" /&gt;
&lt;/beans:bean&gt;
</code></pre>

<p></p>

<p>ItemReader:</p>

<pre><code>@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
@Component
public class MyItemReader implements ItemReader&lt;Integer&gt; {

    JobService jobService;

    private List&lt;Integer&gt; itemsList;

    @Autowired
    public AssignLeadsJobItemReader(@Value("#{jobParameters['jobKey']}") final String jobKey) {

        MyJob alj = jobService.loadByKey(jobKey);

        itemsList = new ArrayList&lt;AssignLeadsJobItem&gt;();

        for(Integer i : myJob.getIdList()) {            
            itemsList.add(i);
        }
    }

    @Override
    public Integer read(){
        if(itemsList == null || itemsList.size() == 0) {
            return null;
        } else {
            return itemsList.remove(0);
        }
    }

    public JobService getJobService() {
        return jobService;
    }

    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }
}
</code></pre>

<p>Job Service:</p>

<pre><code>@Service
@Transactional
public class JobService {
    @Autowired
    protected JobDAO jobDao;

    @Autowired
    protected SpringBatchService springBatchService;

    public JobExecution startJob(String jobName, String jobKey) {    
            // Defined in a different Class without transactions.
            return springBatchService.startJob("job_name", jobKey);
    }

    public MyJob loadByKey(String jobKey) {
            return jobDao.loadByKey(jobKey);
    }
}
</code></pre>

<p><strong>UPDATE 1</strong>
I noticed that the error was caused by calling the reference on the ItemReader constructor. I changed the code thusly:</p>

<pre><code>@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
@Component
public class MyItemReader implements ItemReader&lt;Integer&gt; {
    private JobService jobService;
    private List&lt;Integer&gt; itemsList;
    private String jobKey;

    @Autowired
    public MyItemReader(@Value("#{jobParameters['jobKey']}") final String jobKey) {
        this.jobKey = jobKey;
        this.itemsList = null;
    }

    @Override
    public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        // ugly ugly ugly
        if (itemsList == null) {
            itemsList = new ArrayList&lt;Integer&gt;();

            MyJob jobData = jobService.loadByKey(jobKey);

            for (Integer i : jobData.getIdList()) {
                itemsList.add(i);
            }
        }

        if (itemsList.isEmpty()) {
            return null;
        } else {
            return itemsList.remove(0);
        }
    }
}
</code></pre>

<p>Is there a way to put that ugly initialization in the constructor?</p>