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: If the configuration is specified as 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
  • Element Details

    • modular

      boolean modular
      Indicate 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 an ApplicationContextFactory.
      Returns:
      boolean indicating whether the configuration is going to be modularized into multiple application contexts. Defaults to false.
      Default:
      false
    • dataSourceRef

      String dataSourceRef
      Set 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 databaseType
      Set 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 transactionManagerRef
      Set 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 executionContextSerializerRef
      Set 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 charset
      The charset to use in the job repository and job explorer
      Returns:
      the charset to use. Defaults to UTF-8.
      Default:
      "UTF-8"
    • tablePrefix

      String tablePrefix
      The Batch tables prefix. Defaults to "BATCH_".
      Returns:
      the Batch table prefix
      Default:
      "BATCH_"
    • maxVarCharLength

      int maxVarCharLength
      The maximum length of exit messages in the database.
      Returns:
      the maximum length of exit messages in the database
      Default:
      2500
    • incrementerFactoryRef

      String incrementerFactoryRef
      The incrementer factory to use in various DAOs.
      Returns:
      the bean name of the incrementer factory to use. Defaults to incrementerFactory.
      Default:
      "incrementerFactory"
    • jobKeyGeneratorRef

      String jobKeyGeneratorRef
      The 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

      String lobHandlerRef
      The 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 clobType
      The type of large objects.
      Returns:
      the type of large objects.
      Default:
      2005
    • isolationLevelForCreate

      String isolationLevelForCreate
      Set 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 taskExecutorRef
      Set 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 conversionServiceRef
      Set 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"