Question

In Spring, beans can be configured to be lazily initialized. Spring Batch jobs are also (Spring-managed) beans. That is, when I configure something like

<sb:job id="dummyJob" job-repository="jobRepository">
    <sb:step id="dummyStep">
        <sb:tasklet ref="dummyTasklet" />
    </sb:step>
</sb:job>

I actually configure a new (Job-typed) bean inside the Spring container.

My issue is I really want my Job beans to be lazily initialized. As they are regular Spring-managed beans, I'd expect I can instruct the Spring context to make them lazy. This is because I have a large number of beans and there are many cases in which, during one execution of my Spring-based application, I only run one job.

But there's no lazy-init property I can set on my <sb:job... \> configuration. Is there any way I can force lazy initialization? If I configure my <beans\> root with default-lazy-init="true", will this also apply to the Job beans?

Was it helpful?

Solution

You have two options here:

  1. Configure your job manually. This would allow you to use the regular lazy-init attributes Spring exposes.
  2. Use the JobScope now available in Spring Batch 3. Spring Batch 3 will be available soon, but the JobScope was available in the last milestone.

OTHER TIPS

Just to elaborate on Michael Minella's answer. I had a similar requirement to lazy initialize the job repository. I am working with Spring Batch 2.1.9. The following is working for me.

<bean id="jobRepository"
     class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
     lazy-init="true">
        <property name="dataSource" ref="jobDataSource"/>
        <property name="transactionManager" ref="jobTransactionManager"/>
</bean>

Note one pitfall I had run into: do not set the databaseType i.e. avoid the following:

<property name="databaseType" value="SQLSERVER"/>

This is bad because it disable the auto-discovery of the database type and breaked my JUnits that works on H2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top