I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

This tutorial is a follow-up to the Introduction to jOOQ with Spring article, covering the ways that jOOQ can be used within a Spring Boot application.

If you have not gone through that tutorial, please take a look at it and follow the instructions in section 2 on Maven Dependencies and in section 3 on Code Generation. This will generate source code for Java classes representing tables in the sample database, including Author, Book and AuthorBook.

2. Maven Configuration

In addition to the dependencies and plugins as in the previous tutorial, several other components need to be included in the Maven POM file to make jOOQ work with Spring Boot.

2.1. Dependency Management

The most common way to make use of Spring Boot is to inherit from the spring-boot-starter-parent project by declaring it in the parent element. However, this method is not always suitable as it imposes an inheritance chain to follow, which may not be what users want in many cases.

This tutorial uses another approach: delegating the dependency management to Spring Boot. To make it happen, just adds the following dependencyManagement element to the POM file:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.3.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.3. Dependencies

In order for Spring Boot to control jOOQ, a dependency on the spring-boot-starter-jooq artifact needs to be declared:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jooq</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

3. Spring Boot Configuration

3.1. Initial Boot Config

Before we get to the jOOQ support, we’re going to start preparing things with Spring Boot.

First, we’re going to take advantage of the persistence support and improvements in Boot and our data access information in the standard application.properties file. That way, we can skip over defining the beans and making these configurable via a separate properties file.

We’ll add the URL and credentials here to define our embedded H2 database:

spring.datasource.url=jdbc:h2:~/jooq
spring.datasource.username=sa
spring.datasource.password=

We’re also going to define a simple Boot application:

@SpringBootApplication
@EnableTransactionManagement
public class Application {
    
}

We’ll leave this one simple and empty and we’ll define all other bean declarations in another configuration class – InitialConfiguration.

3.2. Bean Configuration

Let’s now define this InitialConfiguration class:

@Configuration
public class InitialConfiguration {
    // Other declarations
}

Spring Boot has automatically generated and configured the dataSource bean based on properties set in the application.properties file, so we do not need to register it manually. The following code lets the auto-configured DataSource bean to be injected into a field, and shows how this bean is used:

@Autowired
private DataSource dataSource;

@Bean
public DataSourceConnectionProvider connectionProvider() {
    return new DataSourceConnectionProvider
      (new TransactionAwareDataSourceProxy(dataSource));
}

Since a bean named transactionManager has also been automatically created and configured by Spring Boot, we do not need to declare any other bean of the DataSourceTransactionManager type as in the previous tutorial to take advantage of Spring transaction support.

A DSLContext bean is created in the same way as in the PersistenceContext class of the preceding tutorial:

@Bean
public DefaultDSLContext dsl() {
    return new DefaultDSLContext(configuration());
}

Lastly, a Configuration implementation needs to be provided to DSLContext. Since Spring Boot is able to recognize the SQL dialect in use through the existence of H2 artifact on the classpath, a dialect configuration is no longer necessary:

public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.set(connectionProvider());
    jooqConfiguration
      .set(new DefaultExecuteListenerProvider(exceptionTransformer()));

    return jooqConfiguration;
}

4. Using Spring Boot with jOOQ

To make the demonstration of Spring Boot support for jOOQ easier to be followed, the test cases in the prequel of this tutorial are reused with a little change to its class-level annotations:

@SpringApplicationConfiguration(Application.class)
@Transactional("transactionManager")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringBootTest {
    // Other declarations
}

It is clear that rather than adopting the @ContextConfiguration annotation, Spring Boot uses @SpringApplicationConfiguration to take advantage of SpringApplicationContextLoader context loader to test applications.

Test methods for inserting, updating and deleting data are exactly the same as in the previous tutorial. Please have a look at section 5 of that article on Using jOOQ with Spring for more information. All the tests should be successfully executed with the new configuration, proving that jOOQ is fully supported by Spring Boot.

5. Conclusion

This tutorial dug deeper into the use of jOOQ with Spring. It introduced the ways for a Spring Boot application to take advantage of jOOQ to interact with a database in a type-safe manner.

The implementation of all these examples and code snippets can be found in a GitHub project.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

  Subscribe  
newest oldest most voted
Notify of
Caleb Cushing
Guest

this is cool and all but shouldn’t you have a test that does something? to verify it works.

Eugen Paraschiv
Guest

Hey Caleb – yes, of course, and there are a few in the codebase, just not included in the article.