Skip to content

Commit

Permalink
Add @ConditionalOnMissingBean for JobRepository
Browse files Browse the repository at this point in the history
Update `BatchAutoConfiguration` so that the `JobRepository` is not
defined when the user provides an appropriate bean.

Fixes gh-43236
  • Loading branch information
philwebb committed Nov 21, 2024
1 parent 3cae5c2 commit 73fc351
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.sql.DataSource;

import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.explore.JobExplorer;
Expand Down Expand Up @@ -129,6 +130,13 @@ protected DataSource getDataSource() {
return this.dataSource;
}

@Bean
@ConditionalOnMissingBean
@Override
public JobRepository jobRepository() throws BatchConfigurationException {
return super.jobRepository();
}

@Override
protected PlatformTransactionManager getTransactionManager() {
return this.transactionManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ void defaultExecutionContextSerializerIsUsed() {
});
}

@Test
void defaultJobRepositoryIsNotCreatedWhenUserDefinedJobRepositoryBean() {
this.contextRunner
.withUserConfiguration(TestConfigurationWithJobRepository.class, EmbeddedDataSourceConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(TestJobRepository.class));
}

private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
mock(JobExplorer.class), mock(JobRepository.class));
Expand Down Expand Up @@ -596,6 +603,16 @@ static class TestConfiguration {

}

@TestAutoConfigurationPackage(City.class)
static class TestConfigurationWithJobRepository {

@Bean
TestJobRepository jobRepository() {
return mock(TestJobRepository.class);
}

}

@Configuration(proxyBeanMethods = false)
static class EntityManagerFactoryConfiguration {

Expand Down Expand Up @@ -880,4 +897,8 @@ ExecutionContextSerializer executionContextSerializer() {

}

interface TestJobRepository extends JobRepository {

}

}

0 comments on commit 73fc351

Please sign in to comment.