Hi, I think DefaultBatchConfiguration has some issues that need to be addressed:
The documentation suggests, that it should be extended
Quote:
A typical usage of this class is as follows:
@Configuration
public class MyJobConfiguration extends DefaultBatchConfiguration {
@Bean
public Job job(JobRepository jobRepository) {
return new JobBuilder("myJob", jobRepository)
// define job flow as needed
.build();
}
}
- This suggests to extend
DefaultBatchConfiguration. But following Composition over Inheritance, you would rather want to import it. - The only reason to extend
DefaultBatchConfigurationshould probably be to override e. g.getTaskExecutor(), allowing you to customize the Batch infrastructure. But then again, following Composition over Inheritance, it might be better to let the user provide a factory bean combined with a customizer. - It uses factory beans like
JobRepositoryFactoryBean, but these are hard coded and can neither be changed nor customized by the user. - The code above suggests to define job beans in your subclass of
DefaultBatchConfiguration. This mixes two things that, IMO, should not be mixed: Batch infrastructure configuration and job configurations. What if you want to have multiple classes that define jobs, would you then extendDefaultBatchConfigurationmultiple times? If you do that, from which class will the beans be taken? Imagine overridinggetTaskExecutor()in multiple subclasses. Only one will win. So as a user, you most likely want to subclassDefaultBatchConfigurationat most once and put jobs into separate configs. Therefore, the current doc might send users on the wrong path.
Beans can't be replaced
Even if customizable bean factories were provided, it would still not work well with Spring Boot AutoConfiguration where users expect beans to be replaceable. See https://github.com/spring-projects/spring-boot/issues/40040
Therefore, if you want to use a different JobRepository bean, you can't use this class and have to specify all the beans on your own.
I'm interested in reading your thoughts. When I find the time, I would also like to provide suggestions. Unfortunately, I can't right now.
Comment From: wilkinsona
Thanks for the feedback but the structure of DefaultBatchConfiguration is out of Spring Boot's control as it's part of Spring Batch which is managed as a separate project.
Comment From: micheljung
Argh, that's where I wanted to create the issue :) thx