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.
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.
I could fix the issue/your repo by:
@EnableBatchProcessing from:
@Configuration
public class ImportJobConfiguration {//...
@Configuration
@EnableBatchProcessing
public class SpringBatchDemo extends DefaultBatchConfiguration {// ...
Reason are the details of BatchAutoConfiguration.
DefaultBatchConfiguration should (currently/always) be annotated @EnableBatchProcessing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With