Annotation Interface EnableBatchProcessing
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Import({org.springframework.batch.core.configuration.annotation.BatchRegistrar.class,ScopeConfiguration.class,org.springframework.batch.core.configuration.annotation.AutomaticJobRegistrarBeanPostProcessor.class,BatchObservabilityBeanPostProcessor.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 { @Bean public Job job(JobRepository jobRepository) { return new JobBuilder("myJob", jobRepository).start(step1()).next(step2()).build(); } @Bean protected Step step1() { ... } @Bean protected Step step2() { ... } }This annotation configures JDBC-based Batch infrastructure beans, so you must provide a
DataSource
and a PlatformTransactionManager
as beans in the application
context.
Note that only one of your configuration classes needs to have the
@EnableBatchProcessing
annotation. Once you have an
@EnableBatchProcessing
class in your configuration, you have an
instance of StepScope
and
JobScope
, so your beans inside steps can
have @Scope("step")
and @Scope("job")
respectively.
You can also use @Autowired
to insert some useful beans into your
context:
- a
JobRepository
(bean name "jobRepository" of typeSimpleJobRepository
) - a
JobLauncher
(bean name "jobLauncher" of typeTaskExecutorJobLauncher
) - a
JobRegistry
(bean name "jobRegistry" of typeMapJobRegistry
) - a
JobExplorer
(bean name "jobExplorer" of typeSimpleJobExplorer
) - a
JobOperator
(bean name "jobOperator" of typeSimpleJobOperator
) - a
JobRegistrySmartInitializingSingleton
(bean name "jobRegistrySmartInitializingSingleton" of typeJobRegistrySmartInitializingSingleton
)
modular=true
, the context also
contains an AutomaticJobRegistrar
. The job registrar is useful for modularizing
your configuration if there are multiple jobs. It works by creating separate child
application contexts to contain job configurations and register those jobs. The jobs
can then create steps and other dependent components without needing to worry about
bean definition name clashes. Beans of type ApplicationContextFactory
are
automatically registered 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, because cyclic configuration dependencies are likely to develop.
For reference, compare the first example shown earlier to the following Spring XML configuration:
<batch>
<job-repository />
<job id="myJob">
<step id="step1" .../>
<step id="step2" .../>
</job>
<beans:bean id="dataSource" .../>
<beans:bean id="transactionManager" .../>
<beans:bean id="jobLauncher" class=
"org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
<beans:property name="jobRepository" ref="jobRepository" />
</beans:bean>
</batch>
- Author:
- Dave Syer, Mahmoud Ben Hassine, Taeik Lim
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe charset to use in the job repository and job explorerint
The type of large objects.Set the conversion service to use in the job repository and job explorer.Set the type of the data source to use in the job repository.Set the data source to use in the job repository and job explorer.Set the execution context serializer to use in the job repository and job explorer.The incrementer factory to use in various DAOs.Set the isolation level for create parameter value.The generator that determines a unique key for identifying job instance objectsSet theJobParametersConverter
to use in the job operator.Deprecated, for removal: This API element is subject to removal in a future version.Since 5.2 with no replacement.int
The maximum length of exit messages in the database.boolean
Indicate whether the configuration is going to be modularized into multiple application contexts.The Batch tables prefix.Set the task executor to use in the job launcher.Set the transaction manager to use in the job repository.
-
Element Details
-
modular
boolean modularIndicate whether the configuration is going to be modularized into multiple application contexts. If true, 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
-
dataSourceRef
String dataSourceRefSet the data source to use in the job repository and job explorer.- Returns:
- the bean name of the data source to use. Default to dataSource.
- Default:
- "dataSource"
-
databaseType
String databaseTypeSet the type of the data source to use in the job repository. The default type will be introspected from the datasource's metadata.- Returns:
- the type of data source.
- Since:
- 5.1
- See Also:
- Default:
- ""
-
transactionManagerRef
String transactionManagerRefSet the transaction manager to use in the job repository.- Returns:
- the bean name of the transaction manager to use. Defaults to transactionManager
- Default:
- "transactionManager"
-
executionContextSerializerRef
String executionContextSerializerRefSet the execution context serializer to use in the job repository and job explorer.- Returns:
- the bean name of the execution context serializer to use. Default to executionContextSerializer.
- Default:
- "executionContextSerializer"
-
charset
String charsetThe charset to use in the job repository and job explorer- Returns:
- the charset to use. Defaults to UTF-8.
- Default:
- "UTF-8"
-
tablePrefix
String tablePrefixThe Batch tables prefix. Defaults to "BATCH_".- Returns:
- the Batch table prefix
- Default:
- "BATCH_"
-
maxVarCharLength
int maxVarCharLengthThe maximum length of exit messages in the database.- Returns:
- the maximum length of exit messages in the database
- Default:
- 2500
-
incrementerFactoryRef
String incrementerFactoryRefThe incrementer factory to use in various DAOs.- Returns:
- the bean name of the incrementer factory to use. Defaults to incrementerFactory.
- Default:
- "incrementerFactory"
-
jobKeyGeneratorRef
String jobKeyGeneratorRefThe generator that determines a unique key for identifying job instance objects- Returns:
- the bean name of the job key generator to use. Defaults to jobKeyGenerator.
- Since:
- 5.1
- Default:
- "jobKeyGenerator"
-
lobHandlerRef
Deprecated, for removal: This API element is subject to removal in a future version.Since 5.2 with no replacement. Scheduled for removal in v6The large object handler to use in job repository and job explorer.- Returns:
- the bean name of the lob handler to use. Defaults to lobHandler.
- Default:
- "lobHandler"
-
clobType
int clobTypeThe type of large objects.- Returns:
- the type of large objects.
- Default:
- 2005
-
isolationLevelForCreate
String isolationLevelForCreateSet the isolation level for create parameter value. Defaults to ISOLATION_SERIALIZABLE.- Returns:
- the value of the isolation level for create parameter
- Default:
- "ISOLATION_SERIALIZABLE"
-
taskExecutorRef
String taskExecutorRefSet the task executor to use in the job launcher.- Returns:
- the bean name of the task executor to use. Defaults to taskExecutor
- Default:
- "taskExecutor"
-
conversionServiceRef
String conversionServiceRefSet the conversion service to use in the job repository and job explorer. This service is used to convert job parameters from String literal to typed values and vice versa.- Returns:
- the bean name of the conversion service to use. Defaults to conversionService
- Default:
- "conversionService"
-
jobParametersConverterRef
String jobParametersConverterRefSet theJobParametersConverter
to use in the job operator.- Returns:
- the bean name of the job parameters converter to use. Defaults to jobParametersConverter
- Default:
- "jobParametersConverter"
-