Skip to content

Commit 73fc351

Browse files
committed
Add @ConditionalOnMissingBean for JobRepository
Update `BatchAutoConfiguration` so that the `JobRepository` is not defined when the user provides an appropriate bean. Fixes gh-43236
1 parent 3cae5c2 commit 73fc351

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.sql.DataSource;
2222

23+
import org.springframework.batch.core.configuration.BatchConfigurationException;
2324
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2425
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
2526
import org.springframework.batch.core.explore.JobExplorer;
@@ -129,6 +130,13 @@ protected DataSource getDataSource() {
129130
return this.dataSource;
130131
}
131132

133+
@Bean
134+
@ConditionalOnMissingBean
135+
@Override
136+
public JobRepository jobRepository() throws BatchConfigurationException {
137+
return super.jobRepository();
138+
}
139+
132140
@Override
133141
protected PlatformTransactionManager getTransactionManager() {
134142
return this.transactionManager;

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ void defaultExecutionContextSerializerIsUsed() {
517517
});
518518
}
519519

520+
@Test
521+
void defaultJobRepositoryIsNotCreatedWhenUserDefinedJobRepositoryBean() {
522+
this.contextRunner
523+
.withUserConfiguration(TestConfigurationWithJobRepository.class, EmbeddedDataSourceConfiguration.class)
524+
.run((context) -> assertThat(context).hasSingleBean(TestJobRepository.class));
525+
}
526+
520527
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
521528
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
522529
mock(JobExplorer.class), mock(JobRepository.class));
@@ -596,6 +603,16 @@ static class TestConfiguration {
596603

597604
}
598605

606+
@TestAutoConfigurationPackage(City.class)
607+
static class TestConfigurationWithJobRepository {
608+
609+
@Bean
610+
TestJobRepository jobRepository() {
611+
return mock(TestJobRepository.class);
612+
}
613+
614+
}
615+
599616
@Configuration(proxyBeanMethods = false)
600617
static class EntityManagerFactoryConfiguration {
601618

@@ -880,4 +897,8 @@ ExecutionContextSerializer executionContextSerializer() {
880897

881898
}
882899

900+
interface TestJobRepository extends JobRepository {
901+
902+
}
903+
883904
}

0 commit comments

Comments
 (0)