Skip to content

Commit

Permalink
Add customizer for jOOQ's configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jan 11, 2021
1 parent d05efe2 commit 0897af0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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;
Expand Down Expand Up @@ -94,28 +95,58 @@ public DefaultDSLContext dslContext(org.jooq.Configuration configuration) {
@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,
ObjectProvider<ExecuteListenerProvider> executeListenerProviders,
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;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import javax.sql.DataSource;

import org.jooq.CharsetProvider;
import org.jooq.ConverterProvider;
import org.jooq.DSLContext;
import org.jooq.ExecuteListener;
import org.jooq.ExecuteListenerProvider;
Expand Down Expand Up @@ -53,6 +55,7 @@

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}.
Expand Down Expand Up @@ -111,6 +114,23 @@ void jooqWithTx() {
}

@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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 0897af0

Please sign in to comment.