How to customise a jOOQ Configuration that is injected using Spring Boot

Starting from Spring Boot 2.5, there’s a handy new callback that you can implement, called DefaultConfigurationCustomizer, where the word DefaultConfiguration corresponds to jOOQ’s DefaultConfiguration. You can simply create a class like this in your project:

import org.jooq.conf.RenderQuotedNames;
import org.jooq.impl.DefaultConfiguration;
import org.springframework.boot.autoconfigure.jooq.*;
import org.springframework.context.annotation.*;

@Configuration
public class Config {
	@Bean
	public DefaultConfigurationCustomizer configurationCustomiser() {
		return (DefaultConfiguration c) -> c.settings()
			.withRenderQuotedNames(
				RenderQuotedNames.EXPLICIT_DEFAULT_UNQUOTED
			);
	}
}

The above callback receives the DefaultConfiguration at its initialisation stage, during which you can still safely mutate it to change any of the defaults, or add additional settings, etc. That’s it!

You can set a breakpoint inside of the lambda to validate when this initialisation is invoked:

Caused by: java.lang.RuntimeException
	at org.jooq.example.spring.Config.lambda$0(Config.java:30)
	at org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration$DslContextConfiguration.lambda$jooqConfiguration$1(JooqAutoConfiguration.java:104)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
	at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258)
	at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration$DslContextConfiguration.jooqConfiguration(JooqAutoConfiguration.java:104)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 119 more

To play around with this example, check out our jOOQ-spring-boot-example on github.

2 thoughts on “How to customise a jOOQ Configuration that is injected using Spring Boot

  1. If you’re using R2DBC, take note that `JooqAutoConfiguration` only works with a traditional `javax.sql.DataSource`, not a `io.r2dbc.spi.ConnectionFactory`. Worse, if you have both available (e.g. for Flyway, which still does not yet support R2DBC), make sure that the `DSLContext` that you’re using is actually the reactive one, not this one that Spring slips between your beans. Ask me how I know :-/

Leave a Reply to László van den Hoek Cancel reply