Annotation Type EnableBatchProcessing
@Target(TYPE) @Retention(RUNTIME) @Documented @Import(BatchConfigurationSelector.class) public @interface EnableBatchProcessing
Enable Spring Batch features and provide a base configuration for setting up batch jobs in an @Configuration class, roughly equivalent to using the
<batch:*>
XML namespace.@Configuration @EnableBatchProcessing @Import(DataSourceConfiguration.class) public class AppConfig { @Autowired private JobBuilderFactory jobs; @Bean public Job job() { return jobs.get("myJob").start(step1()).next(step2()).build(); } @Bean protected Step step1() { ... } @Bean protected Step step2() { ... } }
The user should to provide aDataSource
as a bean in the context, or else implementBatchConfigurer
in the configuration class itself, e.g.@Configuration @EnableBatchProcessing public class AppConfig extends DefaultBatchConfigurer { @Bean public Job job() { ... } @Override protected JobRepository createJobRepository() { ... } ... }
If a user does not provide aDataSource
within the context, a Map basedJobRepository
will be used. If multipleDataSource
s are defined in the context, the one annotated withPrimary
will be used (Note that if none of them is annotated withPrimary
, the one nameddataSource
will be used if any, otherwise aUnsatisfiedDependencyException
will be thrown). Note that only one of your configuration classes needs to have the@EnableBatchProcessing
annotation. Once you have an@EnableBatchProcessing
class in your configuration you will have an instance ofStepScope
andJobScope
so your beans inside steps can have@Scope("step")
and@Scope("job")
respectively. You will also be able to@Autowired
some useful stuff into your context:- a
JobRepository
(bean name "jobRepository") - a
JobLauncher
(bean name "jobLauncher") - a
JobRegistry
(bean name "jobRegistry") - a
JobExplorer
(bean name "jobExplorer") - a
PlatformTransactionManager
(bean name "transactionManager") - a
JobBuilderFactory
(bean name "jobBuilders") as a convenience to prevent you from having to inject the job repository into every job, as in the examples above - a
StepBuilderFactory
(bean name "stepBuilders") as a convenience to prevent you from having to inject the job repository and transaction manager into every step
ResourcelessTransactionManager
if noDataSource
is provided within the contextDataSourceTransactionManager
if aDataSource
is provided within the context
BatchConfigurer
should be provided. For example:@Configuration @EnableBatchProcessing public class AppConfig extends DefaultBatchConfigurer { @Bean public Job job() { ... } @Override public PlatformTransactionManager getTransactionManager() { return new MyTransactionManager(); } ... }
If the configuration is specified asmodular=true
then the context will also contain anAutomaticJobRegistrar
. The job registrar is useful for modularizing your configuration if there are multiple jobs. It works by creating separate child application contexts containing job configurations and registering those jobs. The jobs can then create steps and other dependent components without needing to worry about bean definition name clashes. Beans of typeApplicationContextFactory
will be registered automatically with the job registrar. Example:@Configuration @EnableBatchProcessing(modular=true) public class AppConfig { @Bean public ApplicationContextFactory someJobs() { return new GenericApplicationContextFactory(SomeJobConfiguration.class); } @Bean public ApplicationContextFactory moreJobs() { return new GenericApplicationContextFactory(MoreJobConfiguration.class); } ... }
Note that a modular parent context in general should not itself contain @Bean definitions for job, especially if aBatchConfigurer
is provided, because cyclic configuration dependencies are otherwise likely to develop.For reference, the first example above can be compared to the following Spring XML configuration:
<batch> <job-repository /> <job id="myJob"> <step id="step1" .../> <step id="step2" .../> </job> <beans:bean id="transactionManager" .../> <beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <beans:property name="jobRepository" ref="jobRepository" /> </beans:bean> </batch>
- Author:
- Dave Syer, Mahmoud Ben Hassine
- a
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description boolean
modular
Indicate whether the configuration is going to be modularized into multiple application contexts.
Element Detail
modular
boolean modular
Indicate whether the configuration is going to be modularized into multiple application contexts. If true then you should not create any @Bean Job definitions in this context, but rather supply them in separate (child) contexts through anApplicationContextFactory
.- Returns:
- boolean indicating whether the configuration is going to be modularized into multiple application contexts. Defaults to false.
- Default:
- false