Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DefaultBatchConfiguration extending in Spring Batch 5 not working

Just to implement a custom Batch Configuration for Spring Batch 5, following the official documentation on Blog: Spring Batch 5 Milestone 6 and JavaDoc Spring Batch 5 Milestone 8, I wrote this code, using Spring Batch 5 via Spring Boot 3 RC1:

    @Configuration
    class MyBatchConfiguration extends DefaultBatchConfiguration {        
        @Override
        protected DataFieldMaxValueIncrementerFactory getIncrementerFactory() {
            return new MyDataFieldMaxValueIncrementerFactory();
        }
    }

But I only get an error about my MyBatchConfiguration#jobRepository Bean, illegally overriding the JobRepositoryFactoryBean#jobRepository Bean. Which is weird, because JobRepositoryFactoryBean has no jobRepository.

Does anyone know how to solve this?

Error:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'jobRepository' defined in class path resource [my/package/MyBatchConfiguration.class]: @Bean definition illegally overridden by existing bean definition: Generic bean: class [org.springframework.batch.core.repository.support.JobRepositoryFactoryBean]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null

Edit: I built a demo project under https://github.com/JD-CSTx/SpringBatchBugJobRepo.

like image 411
Jan Avatar asked Jan 23 '26 23:01

Jan


2 Answers

In your example, you are using both EnableBatchProcessing and DefaultBatchConfiguration, which is not correct. From the documentation of v5, you need to use either EnableBatchProcessing or DefaultBatchConfiguration, but not both:

    
@EnableBatchProcessing should not be used with DefaultBatchConfiguration.
You should either use the declarative way of configuring Spring Batch
through @EnableBatchProcessing, or use the programmatic way of extending
DefaultBatchConfiguration, but not both ways at the same time.

Spring Boot also has a note about this here.

like image 94
Mahmoud Ben Hassine Avatar answered Jan 26 '26 14:01

Mahmoud Ben Hassine


I could fix the issue/your repo by:

  • Moving @EnableBatchProcessing from:
    @Configuration
    public class ImportJobConfiguration {//...
    
  • To:
    @Configuration
    @EnableBatchProcessing
    public class SpringBatchDemo extends DefaultBatchConfiguration {// ...
    

Reason are the details of BatchAutoConfiguration.

DefaultBatchConfiguration should (currently/always) be annotated @EnableBatchProcessing.

like image 43
xerx593 Avatar answered Jan 26 '26 14:01

xerx593