diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index e09ab3e8de0e..0068e6397ec7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -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; @@ -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; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 8f10bea4e035..3595227c959d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -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)); @@ -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 { @@ -880,4 +897,8 @@ ExecutionContextSerializer executionContextSerializer() { } + interface TestJobRepository extends JobRepository { + + } + }