Skip to content

Prefer composition over inheritance in default batch configuration #4962

@fmbenhassine

Description

@fmbenhassine

Spring Batch has always provided a default way to configure infrastructure beans (historically DefaultBatchConfigurer in v3 and v4 which was slightly improved and muted to DefaultBatchConfiguration in v5). However, the way this default configuration is designed to be customized through inheritance is problematic (for all the known limitations of inheritance like multiple inheritance for instance).

In v6, I suggest to change the way the default configuration is used: instead of extending it, it should rather be imported. Therefore, users can compose the batch configuration by importing classes rather than extending them. For example, instead of doing this:

@Configuration
public class MyJobConfiguration extends DefaultBatchConfiguration {
 
   @Bean
   public Job job(JobRepository jobRepository) {
      return new JobBuilder("myJob", jobRepository)
          // define job flow as needed
         .build();
   }
 
}

users can do this:

@Configuration
@Import(DefaultBatchConfiguration.class)
public class MyJobConfiguration {

   @Bean
   public Job job(JobRepository jobRepository) {
      return new JobBuilder("myJob", jobRepository)
         // define job flow as needed
         .build();
   }
 
}

If custom batch scopes are needed, they can be imported in addition to the default configuration:

@Configuration
@Import({DefaultBatchConfiguration.class, ScopeConfiguration.class})
public class MyJobConfiguration {
 
   @Bean
   public Job job(JobRepository jobRepository) {
      return new JobBuilder("myJob", jobRepository)
         // define job flow as needed
         .build();
   }
 
}

This way, the configuration can be composed (with fine-grained components) rather than inherited from a base class and customised by overriding getters. Customization can be achieved by bean lookup from the application context (similar to / consistent with @EnableBatchProcessing).

Metadata

Metadata

Assignees

Labels

api: breaking-changeIssues that require or introduce an API breaking changein: corestatus: for-internal-teamIssues that are planned to be resolved by the Spring Batch team, and not open for contributions.type: feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions