Add `@ConditionalOnMissingBean` for `JobRepository`
Update `BatchAutoConfiguration` so that the `JobRepository` is not defined when the user provides an appropriate bean. Fixes gh-43236
This commit is contained in:
parent
3cae5c27d1
commit
73fc351d71
|
@ -20,6 +20,7 @@ import java.util.List;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
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.annotation.EnableBatchProcessing;
|
||||||
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
|
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
|
||||||
import org.springframework.batch.core.explore.JobExplorer;
|
import org.springframework.batch.core.explore.JobExplorer;
|
||||||
|
@ -129,6 +130,13 @@ public class BatchAutoConfiguration {
|
||||||
return this.dataSource;
|
return this.dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@Override
|
||||||
|
public JobRepository jobRepository() throws BatchConfigurationException {
|
||||||
|
return super.jobRepository();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PlatformTransactionManager getTransactionManager() {
|
protected PlatformTransactionManager getTransactionManager() {
|
||||||
return this.transactionManager;
|
return this.transactionManager;
|
||||||
|
|
|
@ -517,6 +517,13 @@ class BatchAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void defaultJobRepositoryIsNotCreatedWhenUserDefinedJobRepositoryBean() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(TestConfigurationWithJobRepository.class, EmbeddedDataSourceConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(TestJobRepository.class));
|
||||||
|
}
|
||||||
|
|
||||||
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
|
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
|
||||||
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
|
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
|
||||||
mock(JobExplorer.class), mock(JobRepository.class));
|
mock(JobExplorer.class), mock(JobRepository.class));
|
||||||
|
@ -596,6 +603,16 @@ class BatchAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestAutoConfigurationPackage(City.class)
|
||||||
|
static class TestConfigurationWithJobRepository {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
TestJobRepository jobRepository() {
|
||||||
|
return mock(TestJobRepository.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
static class EntityManagerFactoryConfiguration {
|
static class EntityManagerFactoryConfiguration {
|
||||||
|
|
||||||
|
@ -880,4 +897,8 @@ class BatchAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TestJobRepository extends JobRepository {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue