From 0897af0dfacbe5ba61c9ca6434cd978e85f77aaf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 11 Jan 2021 14:16:34 +0100 Subject: [PATCH] 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 --- .../jooq/DefaultConfigurationCustomizer.java | 37 +++++++++++ .../jooq/JooqAutoConfiguration.java | 61 ++++++++++++++----- .../jooq/JooqAutoConfigurationTests.java | 20 ++++++ .../docs/asciidoc/spring-boot-features.adoc | 15 +---- 4 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/DefaultConfigurationCustomizer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/DefaultConfigurationCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/DefaultConfigurationCustomizer.java new file mode 100644 index 0000000000..54e074db14 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/DefaultConfigurationCustomizer.java @@ -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); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java index da2f9265c0..307795610c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java @@ -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, + DataSource dataSource, ObjectProvider 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, ObjectProvider recordMapperProvider, ObjectProvider recordUnmapperProvider, ObjectProvider settings, ObjectProvider recordListenerProviders, @@ -102,20 +114,39 @@ public class JooqAutoConfiguration { ObjectProvider visitListenerProviders, ObjectProvider transactionListenerProviders, ObjectProvider 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; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java index e9d7137e52..bbccf866ff 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java @@ -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, diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 9ec14236aa..3833ae9c1b 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -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.