Skip to content

Commit 148c5e8

Browse files
committed
Polish "Add property spring.data.jdbc.dialect"
See gh-39941
1 parent b3ddb22 commit 148c5e8

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
* Configuration properties for Spring Data JDBC.
2424
*
2525
* @author Jens Schauder
26-
* @since 3.3
26+
* @since 3.3.0
2727
*/
2828
@ConfigurationProperties(prefix = "spring.data.jdbc")
2929
public class JdbcDataProperties {
3030

3131
/**
32-
* Dialect to use. By default, the dialect is determined by inspecting the database connection.
32+
* Dialect to use. By default, the dialect is determined by inspecting the database
33+
* connection.
3334
*/
3435
private Class<? extends Dialect> dialect;
3536

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

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

19-
import java.lang.reflect.InvocationTargetException;
2019
import java.util.Optional;
2120
import java.util.Set;
2221

23-
import org.springframework.beans.factory.BeanCreationException;
22+
import org.springframework.beans.BeanUtils;
2423
import org.springframework.boot.autoconfigure.AutoConfiguration;
2524
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2625
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -149,16 +148,8 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
149148
@Bean
150149
@ConditionalOnMissingBean
151150
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-
}
151+
if (this.properties.getDialect() != null) {
152+
return BeanUtils.instantiateClass(this.properties.getDialect(), Dialect.class);
162153
}
163154
return super.jdbcDialect(operations);
164155
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,11 @@ void allowsUserToDefineCustomDialect() {
186186
@Test
187187
void allowsConfigurationOfDialectByProperty() {
188188
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-
});
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));
196194
}
197195

198196
private void allowsUserToDefineCustomBean(Class<?> configuration, Class<?> beanType, String beanName) {

0 commit comments

Comments
 (0)