3
votes

I have a spring batch application where I have to override beans like jobLauncher. But when I tried to run the command clean install I am getting the bean override issue. It is working fine on my local machine but I am getting the issue over Jenkins, I don't know why it is not happening on my local machine.

I looked into the spring code they are extending the configuration class and overriding the bean. I tried to extend the SimpleBatchConfiguration class but I faced some issue and I don't think it's a good idea.

JobLaunher Bean in Application:

    @Bean
    public JobLauncher jobLauncher(JobRepository jobRepository) throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

JobLauncher in Spring Batch:

    @Override
    @Bean
    public JobLauncher jobLauncher() throws Exception {
        return createLazyProxy(jobLauncher, JobLauncher.class);
    }

Error Logs:


APPLICATION FAILED TO START
***************************

Description:

The bean 'jobLauncher', defined in com.orange.alc.dabek.dataload.config.BatchConfiguration, could not be registered. A bean with that name has already been defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

2019-05-20 20:26:45.056 ERROR 12892 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@29f8134] to prepare test instance [com.orange.alc.dabek.dataload.job.PhoneJobTest@611a2d82]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at 

I want to override the spring batch beans. I found that it's not a good approach to use spring.main.allow-bean-definition-overriding=true because It also hides some benefit. I also don't find the same property in my application.yml. Please let me know about the better solution.

1

1 Answers

9
votes

In order to customize Spring Batch infrastructure beans (job repository, job launcher, transaction manager, etc), you need to provide a custom BatchConfigurer (See the JavaConfig section of the reference documentation).

You can make your batch configuration class extend org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer from Spring Boot or org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer from Spring Batch and override the createJobLauncher method.