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
pull/17660/head
Stephane Nicoll 5 years ago
parent 97907aac30
commit 858199a2fa

@ -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)

@ -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())

Loading…
Cancel
Save