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
This commit is contained in:
parent
24223075eb
commit
b3ddb22f75
|
|
@ -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;
|
||||||
|
import org.springframework.data.relational.core.dialect.Dialect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 Class<? extends Dialect> dialect;
|
||||||
|
|
||||||
|
public Class<? extends Dialect> getDialect() {
|
||||||
|
return this.dialect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDialect(Class<? extends Dialect> dialect) {
|
||||||
|
this.dialect = dialect;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,9 +16,11 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.data.jdbc;
|
package org.springframework.boot.autoconfigure.data.jdbc;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
|
@ -28,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.autoconfigure.domain.EntityScanner;
|
import org.springframework.boot.autoconfigure.domain.EntityScanner;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -59,6 +62,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
|
* @author Jens Schauder
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
* @see EnableJdbcRepositories
|
* @see EnableJdbcRepositories
|
||||||
*/
|
*/
|
||||||
|
|
@ -67,6 +71,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
@ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class })
|
@ConditionalOnClass({ NamedParameterJdbcOperations.class, AbstractJdbcConfiguration.class })
|
||||||
@ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true",
|
@ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true",
|
||||||
matchIfMissing = true)
|
matchIfMissing = true)
|
||||||
|
@EnableConfigurationProperties(JdbcDataProperties.class)
|
||||||
public class JdbcRepositoriesAutoConfiguration {
|
public class JdbcRepositoriesAutoConfiguration {
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
|
@ -82,8 +87,11 @@ public class JdbcRepositoriesAutoConfiguration {
|
||||||
|
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
SpringBootJdbcConfiguration(ApplicationContext applicationContext) {
|
private final JdbcDataProperties properties;
|
||||||
|
|
||||||
|
SpringBootJdbcConfiguration(ApplicationContext applicationContext, JdbcDataProperties properties) {
|
||||||
this.applicationContext = applicationContext;
|
this.applicationContext = applicationContext;
|
||||||
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -141,6 +149,17 @@ public class JdbcRepositoriesAutoConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
|
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
|
||||||
|
if (this.properties.getDialect() != null
|
||||||
|
) {
|
||||||
|
Class<?> dialectType = this.properties.getDialect();
|
||||||
|
try {
|
||||||
|
return (Dialect)dialectType.getDeclaredConstructor().newInstance();
|
||||||
|
}
|
||||||
|
catch (InstantiationException | IllegalAccessException |
|
||||||
|
InvocationTargetException | NoSuchMethodException e) {
|
||||||
|
throw new BeanCreationException("Couldn't create instance of type " + dialectType, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
return super.jdbcDialect(operations);
|
return super.jdbcDialect(operations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.DataAccessStrategy;
|
||||||
import org.springframework.data.jdbc.core.convert.JdbcConverter;
|
import org.springframework.data.jdbc.core.convert.JdbcConverter;
|
||||||
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
|
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.core.mapping.JdbcMappingContext;
|
||||||
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
|
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
|
||||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
||||||
|
|
@ -58,6 +59,7 @@ import static org.mockito.Mockito.mock;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
|
* @author Jens Schauder
|
||||||
*/
|
*/
|
||||||
class JdbcRepositoriesAutoConfigurationTests {
|
class JdbcRepositoriesAutoConfigurationTests {
|
||||||
|
|
||||||
|
|
@ -181,6 +183,18 @@ class JdbcRepositoriesAutoConfigurationTests {
|
||||||
allowsUserToDefineCustomBean(DialectConfiguration.class, Dialect.class, "customDialect");
|
allowsUserToDefineCustomBean(DialectConfiguration.class, Dialect.class, "customDialect");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void allowsConfigurationOfDialectByProperty() {
|
||||||
|
this.contextRunner.with(database())
|
||||||
|
.withPropertyValues("spring.data.jdbc.dialect:" + JdbcPostgresDialect.class.getName())
|
||||||
|
.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) {
|
private void allowsUserToDefineCustomBean(Class<?> configuration, Class<?> beanType, String beanName) {
|
||||||
this.contextRunner.with(database())
|
this.contextRunner.with(database())
|
||||||
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
|
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue