From 7eb9843e1ad5889de05e3053d16a5c8b0b7d71d9 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 14 Mar 2024 10:06:04 +0100 Subject: [PATCH 1/2] Add property spring.data.jdbc.dialect See gh-39941 --- .../data/jdbc/JdbcDataProperties.java | 44 +++++++ .../data/jdbc/JdbcDatabaseDialect.java | 120 ++++++++++++++++++ .../JdbcRepositoriesAutoConfiguration.java | 12 +- ...dbcRepositoriesAutoConfigurationTests.java | 12 ++ 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java new file mode 100644 index 00000000000..4c3a5808bfa --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java @@ -0,0 +1,44 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.data.jdbc; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for Spring Data JDBC. + * + * @author Jens Schauder + * @since 3.3 + */ +@ConfigurationProperties(prefix = "spring.data.jdbc") +public class JdbcDataProperties { + + /** + * Dialect to use. By default, the dialect is determined by inspecting the database + * connection. + */ + private JdbcDatabaseDialect dialect; + + public JdbcDatabaseDialect getDialect() { + return this.dialect; + } + + public void setDialect(JdbcDatabaseDialect dialect) { + this.dialect = dialect; + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java new file mode 100644 index 00000000000..721914f1a9d --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java @@ -0,0 +1,120 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.data.jdbc; + +import java.util.function.Supplier; + +import org.springframework.data.jdbc.core.dialect.JdbcDb2Dialect; +import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect; +import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect; +import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect; +import org.springframework.data.relational.core.dialect.Dialect; +import org.springframework.data.relational.core.dialect.H2Dialect; +import org.springframework.data.relational.core.dialect.HsqlDbDialect; +import org.springframework.data.relational.core.dialect.MariaDbDialect; +import org.springframework.data.relational.core.dialect.OracleDialect; + +/** + * List of database dialects that can be configured in Boot for use with Spring Data JDBC. + * + * @author Jens Schauder + * @since 3.3 + */ +public enum JdbcDatabaseDialect implements Supplier { + + /** + * Provides an instance of {@link JdbcDb2Dialect}. + */ + DB2 { + @Override + public Dialect get() { + return JdbcDb2Dialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link H2Dialect}. + */ + H2 { + @Override + public Dialect get() { + return H2Dialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link HsqlDbDialect}. + */ + HSQL { + @Override + public Dialect get() { + return HsqlDbDialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link MariaDbDialect}. + */ + MARIA { + @Override + public Dialect get() { + return MariaDbDialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link JdbcMySqlDialect}. + */ + MYSQL { + @Override + public Dialect get() { + return JdbcMySqlDialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link OracleDialect}. + */ + ORACLE { + @Override + public Dialect get() { + return OracleDialect.INSTANCE; + + } + }, + + /** + * Provides an instance of {@link JdbcPostgresDialect}. + */ + POSTGRESQL { + @Override + public Dialect get() { + return JdbcPostgresDialect.INSTANCE; + } + }, + + /** + * Provides an instance of {@link JdbcSqlServerDialect}. + */ + SQL_SERVER { + @Override + public Dialect get() { + return JdbcSqlServerDialect.INSTANCE; + } + } + +} 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 b17bd3bf40f..6438e20b0aa 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 @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.domain.EntityScanner; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -59,6 +60,7 @@ import org.springframework.transaction.PlatformTransactionManager; * @author Andy Wilkinson * @author Stephane Nicoll * @author Mark Paluch + * @author Jens Schauder * @since 2.1.0 * @see EnableJdbcRepositories */ @@ -67,6 +69,7 @@ import org.springframework.transaction.PlatformTransactionManager; @ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class }) @ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true", matchIfMissing = true) +@EnableConfigurationProperties(JdbcDataProperties.class) public class JdbcRepositoriesAutoConfiguration { @Configuration(proxyBeanMethods = false) @@ -82,8 +85,11 @@ public class JdbcRepositoriesAutoConfiguration { private final ApplicationContext applicationContext; - SpringBootJdbcConfiguration(ApplicationContext applicationContext) { + private final JdbcDataProperties properties; + + SpringBootJdbcConfiguration(ApplicationContext applicationContext, JdbcDataProperties properties) { this.applicationContext = applicationContext; + this.properties = properties; } @Override @@ -141,6 +147,10 @@ public class JdbcRepositoriesAutoConfiguration { @Bean @ConditionalOnMissingBean public Dialect jdbcDialect(NamedParameterJdbcOperations operations) { + JdbcDatabaseDialect dialectEnum = this.properties.getDialect(); + if (dialectEnum != null) { + return dialectEnum.get(); + } return super.jdbcDialect(operations); } 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 064d5d34c5a..27353b77961 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 @@ -40,6 +40,7 @@ import org.springframework.data.jdbc.core.JdbcAggregateTemplate; import org.springframework.data.jdbc.core.convert.DataAccessStrategy; import org.springframework.data.jdbc.core.convert.JdbcConverter; import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; +import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect; import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; @@ -58,6 +59,7 @@ import static org.mockito.Mockito.mock; * @author Andy Wilkinson * @author Stephane Nicoll * @author Mark Paluch + * @author Jens Schauder */ class JdbcRepositoriesAutoConfigurationTests { @@ -181,6 +183,16 @@ class JdbcRepositoriesAutoConfigurationTests { allowsUserToDefineCustomBean(DialectConfiguration.class, Dialect.class, "customDialect"); } + @Test + void allowsConfigurationOfDialectByProperty() { + this.contextRunner.with(database()) + .withPropertyValues("spring.data.jdbc.dialect=postgresql") + .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class, + DataSourceTransactionManagerAutoConfiguration.class)) + .withUserConfiguration(TestConfiguration.class) + .run((context) -> assertThat(context).hasSingleBean(JdbcPostgresDialect.class)); + } + private void allowsUserToDefineCustomBean(Class configuration, Class beanType, String beanName) { this.contextRunner.with(database()) .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class, From 649d4be51dd3cbf351566b7a1f91f5e1b27dd05e Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Fri, 22 Mar 2024 11:43:09 +0100 Subject: [PATCH 2/2] Polish "Add property spring.data.jdbc.dialect" See gh-39941 --- .../boot/autoconfigure/data/jdbc/JdbcDataProperties.java | 2 +- .../boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java | 2 +- .../data/jdbc/JdbcRepositoriesAutoConfiguration.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java index 4c3a5808bfa..e95c4d04276 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java @@ -22,7 +22,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * Configuration properties for Spring Data JDBC. * * @author Jens Schauder - * @since 3.3 + * @since 3.3.0 */ @ConfigurationProperties(prefix = "spring.data.jdbc") public class JdbcDataProperties { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java index 721914f1a9d..0aa7c421f7f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDatabaseDialect.java @@ -32,7 +32,7 @@ import org.springframework.data.relational.core.dialect.OracleDialect; * List of database dialects that can be configured in Boot for use with Spring Data JDBC. * * @author Jens Schauder - * @since 3.3 + * @since 3.3.0 */ public enum JdbcDatabaseDialect implements Supplier { 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 6438e20b0aa..e64a3089d57 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 @@ -147,9 +147,9 @@ public class JdbcRepositoriesAutoConfiguration { @Bean @ConditionalOnMissingBean public Dialect jdbcDialect(NamedParameterJdbcOperations operations) { - JdbcDatabaseDialect dialectEnum = this.properties.getDialect(); - if (dialectEnum != null) { - return dialectEnum.get(); + JdbcDatabaseDialect dialect = this.properties.getDialect(); + if (dialect != null) { + return dialect.get(); } return super.jdbcDialect(operations); }