From 858199a2fa14f835369ede30deb3de7fdfcc98c9 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 28 Jul 2019 09:59:19 +0200 Subject: [PATCH] Only configure Spring Data jdbc if a transaction manager is present This commit adds an extra condition to JdbcRepositoriesAutoConfiguration so that it doesn't scan for repositories if a transaction manager is not available as this dependency is now mandatory. Closes gh-17654 --- .../jdbc/JdbcRepositoriesAutoConfiguration.java | 7 +++++-- .../JdbcRepositoriesAutoConfigurationTests.java | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java index da0e2c1a7a..dbf2358af5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -30,6 +31,7 @@ import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; import org.springframework.data.jdbc.repository.config.JdbcConfiguration; import org.springframework.data.jdbc.repository.config.JdbcRepositoryConfigExtension; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; +import org.springframework.transaction.PlatformTransactionManager; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JDBC Repositories. @@ -39,16 +41,17 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; * providing an {@link AbstractJdbcConfiguration} subclass. * * @author Andy Wilkinson + * @author Stephane Nicoll * @since 2.1.0 * @see EnableJdbcRepositories */ @SuppressWarnings("deprecation") @Configuration(proxyBeanMethods = false) -@ConditionalOnBean(NamedParameterJdbcOperations.class) +@ConditionalOnBean({ NamedParameterJdbcOperations.class, PlatformTransactionManager.class }) @ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class }) @ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true", matchIfMissing = true) -@AutoConfigureAfter(JdbcTemplateAutoConfiguration.class) +@AutoConfigureAfter({ JdbcTemplateAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class }) public class JdbcRepositoriesAutoConfiguration { @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java index 345ab86eed..e947b1e992 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java @@ -34,8 +34,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; -import org.springframework.data.jdbc.repository.config.JdbcRepositoryConfigExtension; import org.springframework.data.repository.Repository; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import static org.assertj.core.api.Assertions.assertThat; @@ -53,17 +53,28 @@ class JdbcRepositoriesAutoConfigurationTests { @Test void backsOffWithNoDataSource() { this.contextRunner.withUserConfiguration(TestConfiguration.class) - .run((context) -> assertThat(context).doesNotHaveBean(JdbcRepositoryConfigExtension.class)); + .run((context) -> assertThat(context).doesNotHaveBean(AbstractJdbcConfiguration.class)); } @Test void backsOffWithNoJdbcOperations() { this.contextRunner.with(database()).withUserConfiguration(TestConfiguration.class).run((context) -> { assertThat(context).hasSingleBean(DataSource.class); - assertThat(context).doesNotHaveBean(JdbcRepositoryConfigExtension.class); + assertThat(context).doesNotHaveBean(AbstractJdbcConfiguration.class); }); } + @Test + void backsOffWithNoTransactionManager() { + this.contextRunner.with(database()) + .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class)) + .withUserConfiguration(TestConfiguration.class).run((context) -> { + assertThat(context).hasSingleBean(DataSource.class); + assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class); + assertThat(context).doesNotHaveBean(AbstractJdbcConfiguration.class); + }); + } + @Test void basicAutoConfiguration() { this.contextRunner.with(database())