From aac1829106446587eb0b023d6ade2e1c93ad91c6 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 26 Jul 2022 15:11:00 -0700 Subject: [PATCH] Make Batch auto-configuration back-off is spring-jdbc isn't present See gh-29487 --- .../autoconfigure/batch/BatchAutoConfiguration.java | 3 ++- .../batch/BatchAutoConfigurationTests.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 83740f27de..a28f21de34 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 @@ -41,6 +41,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.util.StringUtils; /** @@ -60,7 +61,7 @@ import org.springframework.util.StringUtils; * @since 1.0.0 */ @AutoConfiguration(after = HibernateJpaAutoConfiguration.class) -@ConditionalOnClass({ JobLauncher.class, DataSource.class }) +@ConditionalOnClass({ JobLauncher.class, DataSource.class, DatabasePopulator.class }) @ConditionalOnBean({ DataSource.class, JobLauncher.class }) @EnableConfigurationProperties(BatchProperties.class) @Import({ BatchConfigurerConfiguration.class, DatabaseInitializationDependencyConfigurer.class }) 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 38fe453ab1..bf3f6ad5cd 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 @@ -61,6 +61,7 @@ import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; import org.springframework.boot.sql.init.DatabaseInitializationMode; import org.springframework.boot.sql.init.DatabaseInitializationSettings; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; @@ -71,6 +72,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @@ -114,6 +116,15 @@ class BatchAutoConfigurationTests { }); } + @Test + void autoconfigurationBacksOffEntirelyIfSpringJdbcAbsent() { + this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class) + .withClassLoader(new FilteredClassLoader(DatabasePopulator.class)).run((context) -> { + assertThat(context).doesNotHaveBean(JobLauncherApplicationRunner.class); + assertThat(context).doesNotHaveBean(BatchDataSourceScriptDatabaseInitializer.class); + }); + } + @Test void testDefinesAndLaunchesJob() { this.contextRunner.withUserConfiguration(JobConfiguration.class, EmbeddedDataSourceConfiguration.class)