Skip to content

Commit b3ddb22

Browse files
schaudermhalbritter
authored andcommitted
Add property spring.data.jdbc.dialect
The class valued property allows to configure a dialect, without relying on a database connection to determine it. See gh-39941
1 parent 2422307 commit b3ddb22

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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
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 connection.
33+
*/
34+
private Class<? extends Dialect> dialect;
35+
36+
public Class<? extends Dialect> getDialect() {
37+
return this.dialect;
38+
}
39+
40+
public void setDialect(Class<? extends Dialect> dialect) {
41+
this.dialect = dialect;
42+
}
43+
44+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package org.springframework.boot.autoconfigure.data.jdbc;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.util.Optional;
2021
import java.util.Set;
2122

23+
import org.springframework.beans.factory.BeanCreationException;
2224
import org.springframework.boot.autoconfigure.AutoConfiguration;
2325
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2426
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -28,6 +30,7 @@
2830
import org.springframework.boot.autoconfigure.domain.EntityScanner;
2931
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
3032
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
33+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3134
import org.springframework.context.ApplicationContext;
3235
import org.springframework.context.annotation.Bean;
3336
import org.springframework.context.annotation.Configuration;
@@ -59,6 +62,7 @@
5962
* @author Andy Wilkinson
6063
* @author Stephane Nicoll
6164
* @author Mark Paluch
65+
* @author Jens Schauder
6266
* @since 2.1.0
6367
* @see EnableJdbcRepositories
6468
*/
@@ -67,6 +71,7 @@
6771
@ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class })
6872
@ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true",
6973
matchIfMissing = true)
74+
@EnableConfigurationProperties(JdbcDataProperties.class)
7075
public class JdbcRepositoriesAutoConfiguration {
7176

7277
@Configuration(proxyBeanMethods = false)
@@ -82,8 +87,11 @@ static class SpringBootJdbcConfiguration extends AbstractJdbcConfiguration {
8287

8388
private final ApplicationContext applicationContext;
8489

85-
SpringBootJdbcConfiguration(ApplicationContext applicationContext) {
90+
private final JdbcDataProperties properties;
91+
92+
SpringBootJdbcConfiguration(ApplicationContext applicationContext, JdbcDataProperties properties) {
8693
this.applicationContext = applicationContext;
94+
this.properties = properties;
8795
}
8896

8997
@Override
@@ -141,6 +149,17 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
141149
@Bean
142150
@ConditionalOnMissingBean
143151
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
152+
if (this.properties.getDialect() != null
153+
) {
154+
Class<?> dialectType = this.properties.getDialect();
155+
try {
156+
return (Dialect)dialectType.getDeclaredConstructor().newInstance();
157+
}
158+
catch (InstantiationException | IllegalAccessException |
159+
InvocationTargetException | NoSuchMethodException e) {
160+
throw new BeanCreationException("Couldn't create instance of type " + dialectType, e);
161+
}
162+
}
144163
return super.jdbcDialect(operations);
145164
}
146165

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

Lines changed: 14 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,18 @@ 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) -> {
194+
assertThat(context).hasSingleBean(JdbcPostgresDialect.class);
195+
});
196+
}
197+
184198
private void allowsUserToDefineCustomBean(Class<?> configuration, Class<?> beanType, String beanName) {
185199
this.contextRunner.with(database())
186200
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,

0 commit comments

Comments
 (0)