Add customizer for jOOQ's configuration

This commit adds a customizer hook point for jOOQ's configuration. When
such a bean is present, it is invoked with the auto-configuration
`Configuration`. This effectively supersedes checking for a number of
jOOQ `*Provider`beans. The existing beans are still honoured, in a
deprecated fashion.

Closes gh-24732
pull/24753/head
Stephane Nicoll 4 years ago
parent d05efe28c6
commit 0897af0dfa

@ -0,0 +1,37 @@
/*
* Copyright 2012-2021 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.jooq;
import org.jooq.impl.DefaultConfiguration;
/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link DefaultConfiguration} whilst retaining default auto-configuration.
*
* @author Stephane Nicoll
* @since 2.5.0
*/
@FunctionalInterface
public interface DefaultConfigurationCustomizer {
/**
* Customize the {@link DefaultConfiguration jOOQ Configuration}.
* @param configuration the configuration to customize
*/
void customize(DefaultConfiguration configuration);
}

@ -45,6 +45,7 @@ import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfigu
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.PlatformTransactionManager;
@ -94,7 +95,18 @@ public class JooqAutoConfiguration {
@Bean
@ConditionalOnMissingBean(org.jooq.Configuration.class)
public DefaultConfiguration jooqConfiguration(JooqProperties properties, ConnectionProvider connectionProvider,
DataSource dataSource, ObjectProvider<TransactionProvider> transactionProvider,
DataSource dataSource, ObjectProvider<DefaultConfigurationCustomizer> configurationCustomizers) {
DefaultConfiguration configuration = new DefaultConfiguration();
configuration.set(properties.determineSqlDialect(dataSource));
configuration.set(connectionProvider);
configurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
return configuration;
}
@Bean
@Deprecated
public DefaultConfigurationCustomizer jooQProviderDefaultConfigurationCustomizer(
ObjectProvider<TransactionProvider> transactionProvider,
ObjectProvider<RecordMapperProvider> recordMapperProvider,
ObjectProvider<RecordUnmapperProvider> recordUnmapperProvider, ObjectProvider<Settings> settings,
ObjectProvider<RecordListenerProvider> recordListenerProviders,
@ -102,20 +114,39 @@ public class JooqAutoConfiguration {
ObjectProvider<VisitListenerProvider> visitListenerProviders,
ObjectProvider<TransactionListenerProvider> transactionListenerProviders,
ObjectProvider<ExecutorProvider> executorProvider) {
DefaultConfiguration configuration = new DefaultConfiguration();
configuration.set(properties.determineSqlDialect(dataSource));
configuration.set(connectionProvider);
transactionProvider.ifAvailable(configuration::set);
recordMapperProvider.ifAvailable(configuration::set);
recordUnmapperProvider.ifAvailable(configuration::set);
settings.ifAvailable(configuration::set);
executorProvider.ifAvailable(configuration::set);
configuration.set(recordListenerProviders.orderedStream().toArray(RecordListenerProvider[]::new));
configuration.set(executeListenerProviders.orderedStream().toArray(ExecuteListenerProvider[]::new));
configuration.set(visitListenerProviders.orderedStream().toArray(VisitListenerProvider[]::new));
configuration.setTransactionListenerProvider(
transactionListenerProviders.orderedStream().toArray(TransactionListenerProvider[]::new));
return configuration;
return new OrderedDefaultConfigurationCustomizer((configuration) -> {
transactionProvider.ifAvailable(configuration::set);
recordMapperProvider.ifAvailable(configuration::set);
recordUnmapperProvider.ifAvailable(configuration::set);
settings.ifAvailable(configuration::set);
executorProvider.ifAvailable(configuration::set);
configuration.set(recordListenerProviders.orderedStream().toArray(RecordListenerProvider[]::new));
configuration.set(executeListenerProviders.orderedStream().toArray(ExecuteListenerProvider[]::new));
configuration.set(visitListenerProviders.orderedStream().toArray(VisitListenerProvider[]::new));
configuration.setTransactionListenerProvider(
transactionListenerProviders.orderedStream().toArray(TransactionListenerProvider[]::new));
});
}
}
private static class OrderedDefaultConfigurationCustomizer implements DefaultConfigurationCustomizer, Ordered {
private final DefaultConfigurationCustomizer delegate;
OrderedDefaultConfigurationCustomizer(DefaultConfigurationCustomizer delegate) {
this.delegate = delegate;
}
@Override
public void customize(DefaultConfiguration configuration) {
this.delegate.customize(configuration);
}
@Override
public int getOrder() {
return 0;
}
}

@ -20,6 +20,8 @@ import java.util.concurrent.Executor;
import javax.sql.DataSource;
import org.jooq.CharsetProvider;
import org.jooq.ConverterProvider;
import org.jooq.DSLContext;
import org.jooq.ExecuteListener;
import org.jooq.ExecuteListenerProvider;
@ -53,6 +55,7 @@ import org.springframework.transaction.PlatformTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link JooqAutoConfiguration}.
@ -111,6 +114,23 @@ class JooqAutoConfigurationTests {
}
@Test
void dslContextWithConfigurationCustomizersAreApplied() {
ConverterProvider converterProvider = mock(ConverterProvider.class);
CharsetProvider charsetProvider = mock(CharsetProvider.class);
this.contextRunner.withUserConfiguration(JooqDataSourceConfiguration.class)
.withBean("configurationCustomizer1", DefaultConfigurationCustomizer.class,
() -> (configuration) -> configuration.set(converterProvider))
.withBean("configurationCustomizer2", DefaultConfigurationCustomizer.class,
() -> (configuration) -> configuration.set(charsetProvider))
.run((context) -> {
DSLContext dsl = context.getBean(DSLContext.class);
assertThat(dsl.configuration().converterProvider()).isSameAs(converterProvider);
assertThat(dsl.configuration().charsetProvider()).isSameAs(charsetProvider);
});
}
@Test
@Deprecated
void customProvidersArePickedUp() {
this.contextRunner.withUserConfiguration(JooqDataSourceConfiguration.class, TxManagerConfiguration.class,
TestRecordMapperProvider.class, TestRecordUnmapperProvider.class, TestRecordListenerProvider.class,

@ -4429,19 +4429,8 @@ NOTE: Spring Boot can only auto-configure dialects supported by the open source
==== Customizing jOOQ
More advanced customizations can be achieved by defining your own `@Bean` definitions, which is used when the jOOQ `Configuration` is created.
You can define beans for the following jOOQ Types:
* `ConnectionProvider`
* `ExecutorProvider`
* `TransactionProvider`
* `RecordMapperProvider`
* `RecordUnmapperProvider`
* `Settings`
* `RecordListenerProvider`
* `ExecuteListenerProvider`
* `VisitListenerProvider`
* `TransactionListenerProvider`
More advanced customizations can be achieved by defining your own `DefaultConfigurationCustomizer` bean that will be invoked prior to creating the `org.jooq.Configuration` `@Bean`.
This takes precedence to anything that is applied by the auto-configuration.
You can also create your own `org.jooq.Configuration` `@Bean` if you want to take complete control of the jOOQ configuration.

Loading…
Cancel
Save