Skip to content

Commit

Permalink
Retain jOOQ configuration bean when used defines a DSLContext bean
Browse files Browse the repository at this point in the history
Closes gh-42400
  • Loading branch information
wilkinsona committed Sep 23, 2024
1 parent 97ff6e2 commit 6806276
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.PlatformTransactionManager;
Expand All @@ -53,6 +52,7 @@
@AutoConfiguration(after = { DataSourceAutoConfiguration.class, TransactionAutoConfiguration.class })
@ConditionalOnClass(DSLContext.class)
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(JooqProperties.class)
public class JooqAutoConfiguration {

@Bean
Expand Down Expand Up @@ -81,31 +81,25 @@ public ExceptionTranslatorExecuteListener jooqExceptionTranslator() {
return ExceptionTranslatorExecuteListener.DEFAULT;
}

@Configuration(proxyBeanMethods = false)
@Bean
@ConditionalOnMissingBean(DSLContext.class)
@EnableConfigurationProperties(JooqProperties.class)
public static class DslContextConfiguration {

@Bean
public DefaultDSLContext dslContext(org.jooq.Configuration configuration) {
return new DefaultDSLContext(configuration);
}

@Bean
@ConditionalOnMissingBean(org.jooq.Configuration.class)
public DefaultConfiguration jooqConfiguration(JooqProperties properties, ConnectionProvider connectionProvider,
DataSource dataSource, ObjectProvider<TransactionProvider> transactionProvider,
ObjectProvider<ExecuteListenerProvider> executeListenerProviders,
ObjectProvider<DefaultConfigurationCustomizer> configurationCustomizers) {
DefaultConfiguration configuration = new DefaultConfiguration();
configuration.set(properties.determineSqlDialect(dataSource));
configuration.set(connectionProvider);
transactionProvider.ifAvailable(configuration::set);
configuration.set(executeListenerProviders.orderedStream().toArray(ExecuteListenerProvider[]::new));
configurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
return configuration;
}
public DefaultDSLContext dslContext(org.jooq.Configuration configuration) {
return new DefaultDSLContext(configuration);
}

@Bean
@ConditionalOnMissingBean(org.jooq.Configuration.class)
public DefaultConfiguration jooqConfiguration(JooqProperties properties, ConnectionProvider connectionProvider,
DataSource dataSource, ObjectProvider<TransactionProvider> transactionProvider,
ObjectProvider<ExecuteListenerProvider> executeListenerProviders,
ObjectProvider<DefaultConfigurationCustomizer> configurationCustomizers) {
DefaultConfiguration configuration = new DefaultConfiguration();
configuration.set(properties.determineSqlDialect(dataSource));
configuration.set(connectionProvider);
transactionProvider.ifAvailable(configuration::set);
configuration.set(executeListenerProviders.orderedStream().toArray(ExecuteListenerProvider[]::new));
configurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
return configuration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jooq.TransactionProvider;
import org.jooq.TransactionalRunnable;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -214,6 +215,12 @@ void transactionProviderFromConfigurationCustomizerOverridesTransactionProviderB
});
}

@Test
void autoConfiguredJooqConfigurationCanBeUsedToCreateCustomDslContext() {
this.contextRunner.withUserConfiguration(CustomDslContextConfiguration.class, JooqDataSourceConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(DSLContext.class).hasBean("customDslContext"));
}

static class AssertFetch implements TransactionalRunnable {

private final DSLContext dsl;
Expand Down Expand Up @@ -305,6 +312,16 @@ PlatformTransactionManager transactionManager(DataSource dataSource) {

}

@Configuration(proxyBeanMethods = false)
static class CustomDslContextConfiguration {

@Bean
DSLContext customDslContext(org.jooq.Configuration configuration) {
return new DefaultDSLContext(configuration);
}

}

@Order(100)
static class TestExecuteListenerProvider implements ExecuteListenerProvider {

Expand Down

0 comments on commit 6806276

Please sign in to comment.