3

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.

2

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.