Skip to content

Commit ab5dee8

Browse files
committed
Merge pull request #39941 from schauder
* pr/39941: Polish "Add property spring.data.jdbc.dialect" Add property spring.data.jdbc.dialect Closes gh-39941
2 parents 2422307 + 148c5e8 commit ab5dee8

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.jdbc;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.data.relational.core.dialect.Dialect;
21+
22+
/**
23+
* Configuration properties for Spring Data JDBC.
24+
*
25+
* @author Jens Schauder
26+
* @since 3.3.0
27+
*/
28+
@ConfigurationProperties(prefix = "spring.data.jdbc")
29+
public class JdbcDataProperties {
30+
31+
/**
32+
* Dialect to use. By default, the dialect is determined by inspecting the database
33+
* connection.
34+
*/
35+
private Class<? extends Dialect> dialect;
36+
37+
public Class<? extends Dialect> getDialect() {
38+
return this.dialect;
39+
}
40+
41+
public void setDialect(Class<? extends Dialect> dialect) {
42+
this.dialect = dialect;
43+
}
44+
45+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Optional;
2020
import java.util.Set;
2121

22+
import org.springframework.beans.BeanUtils;
2223
import org.springframework.boot.autoconfigure.AutoConfiguration;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -28,6 +29,7 @@
2829
import org.springframework.boot.autoconfigure.domain.EntityScanner;
2930
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
3031
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3133
import org.springframework.context.ApplicationContext;
3234
import org.springframework.context.annotation.Bean;
3335
import org.springframework.context.annotation.Configuration;
@@ -59,6 +61,7 @@
5961
* @author Andy Wilkinson
6062
* @author Stephane Nicoll
6163
* @author Mark Paluch
64+
* @author Jens Schauder
6265
* @since 2.1.0
6366
* @see EnableJdbcRepositories
6467
*/
@@ -67,6 +70,7 @@
6770
@ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class })
6871
@ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true",
6972
matchIfMissing = true)
73+
@EnableConfigurationProperties(JdbcDataProperties.class)
7074
public class JdbcRepositoriesAutoConfiguration {
7175

7276
@Configuration(proxyBeanMethods = false)
@@ -82,8 +86,11 @@ static class SpringBootJdbcConfiguration extends AbstractJdbcConfiguration {
8286

8387
private final ApplicationContext applicationContext;
8488

85-
SpringBootJdbcConfiguration(ApplicationContext applicationContext) {
89+
private final JdbcDataProperties properties;
90+
91+
SpringBootJdbcConfiguration(ApplicationContext applicationContext, JdbcDataProperties properties) {
8692
this.applicationContext = applicationContext;
93+
this.properties = properties;
8794
}
8895

8996
@Override
@@ -141,6 +148,9 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
141148
@Bean
142149
@ConditionalOnMissingBean
143150
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
151+
if (this.properties.getDialect() != null) {
152+
return BeanUtils.instantiateClass(this.properties.getDialect(), Dialect.class);
153+
}
144154
return super.jdbcDialect(operations);
145155
}
146156

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
4141
import org.springframework.data.jdbc.core.convert.JdbcConverter;
4242
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
43+
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
4344
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4445
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
4546
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
@@ -58,6 +59,7 @@
5859
* @author Andy Wilkinson
5960
* @author Stephane Nicoll
6061
* @author Mark Paluch
62+
* @author Jens Schauder
6163
*/
6264
class JdbcRepositoriesAutoConfigurationTests {
6365

@@ -181,6 +183,16 @@ void allowsUserToDefineCustomDialect() {
181183
allowsUserToDefineCustomBean(DialectConfiguration.class, Dialect.class, "customDialect");
182184
}
183185

186+
@Test
187+
void allowsConfigurationOfDialectByProperty() {
188+
this.contextRunner.with(database())
189+
.withPropertyValues("spring.data.jdbc.dialect:" + JdbcPostgresDialect.class.getName())
190+
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
191+
DataSourceTransactionManagerAutoConfiguration.class))
192+
.withUserConfiguration(TestConfiguration.class)
193+
.run((context) -> assertThat(context).hasSingleBean(JdbcPostgresDialect.class));
194+
}
195+
184196
private void allowsUserToDefineCustomBean(Class<?> configuration, Class<?> beanType, String beanName) {
185197
this.contextRunner.with(database())
186198
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,

0 commit comments

Comments
 (0)