3

I created a simple project with spring boot and JOOQ, added dependency "spring-boot-starter-jooq". The application failed to start when I compiled.

Here is the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bingo</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>api</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hashids</groupId>
            <artifactId>hashids</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <configuration>
                    <jdbc>
                        <driver>com.mysql.jdbc.Driver</driver>
                        <url>jdbc:mysql://123.123.123.123:3306/dbname</url>
                        <user>root</user>
                        <password>password</password>
                    </jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.util.mysql.MySQLDatabase</name>
                            <includes>.*</includes>
                            <excludes></excludes>
                            <inputSchema>dbname</inputSchema>
                            <forcedTypes>
                                <forcedType>
                                    <name>CustomUUID</name>
                                    <types>binary.*</types>
                                </forcedType>
                                <forcedType>
                                    <name>Geometry</name>
                                    <types>(geometry|GEOMETRY)</types>
                                </forcedType>
                            </forcedTypes>
                            <properties>
                                <property>
                                    <key>packages</key>
                                    <value>com.bingo.api.entity</value>
                                </property>
                            </properties>
                        </database>
                        <generate>
                            <pojos>true</pojos>
                        </generate>
                    </generator>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This is where I want to inject DSLContext:

@Service
public class GameDao {
    private final DSLContext create;
    @Autowired
    public GameDao(DSLContext dslContext) {
        this.create = dslContext;
    }
}

Error Message:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gameDao' defined in file [/Users/yangzhiwei/Downloads/BingoApi/target/classes/com/bingo/api/dao/GameDao.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.jooq.DSLContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

/*************************** 

APPLICATION FAILED TO START

/*************************** 

Description:  Parameter 0 of constructor in com.bingo.api.dao.GameDao required a bean of type 'org.jooq.DSLContext' that could not be found. 
Action: Consider defining a bean of type 'org.jooq.DSLContext' in your configuration.

Any help please?

Update: 1.I added properties in application.properties file like this:

spring.datasource.url==jdbc:mysql://123.123.123.123:3306/bingo
spring.datasource.user=username
spring.datasource.password=password

2.I added jooqConfiguration class. Now got another error message:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jooqConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource in com.bingo.api.config.JooqConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'
invzbl3
  • 5,872
  • 9
  • 36
  • 76
croul
  • 51
  • 1
  • 4
  • The `spring.datasource` configuration should be enough. You have an extra `=` sign in `spring.datasource.url`. – Strelok Mar 06 '18 at 04:04

4 Answers4

7

One reason why people using Spring Boot 2.4 or 2.5 with jOOQ 3.15+ may run into this message:

No qualifying bean of type 'org.jooq.DSLContext' available: expected at least 1 bean which qualifies as autowire candidate.

... is because jOOQ 3.15 has a new R2DBC dependency, and Spring Boot 2.4 / 2.5 isn't ready yet for third parties that work with both JDBC and R2DBC, see also How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0

Most users aren't using R2DBC, so you can just exclude the R2dbcAutoConfiguration (watch out for typos. It's not R2dbcDataAutoConfiguration):

@SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
4

You need to instantiate the DSLContext.

First add the properties:

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

EDIT: As per jhyot's comment... adding the configuration should be good enough, since the DSLContext will be created for you by JooqAutoConfiguration.

Optional: Add the configuration to create the DSLContext bean. Here is an example.

@Configuration
public class JooqConfiguration {

    @Autowired
    private DataSource dataSource;

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

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

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

        return jooqConfiguration;
    }

}

Reference: http://www.baeldung.com/spring-boot-support-for-jooq

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • 1
    You actually don't need to create the DSLContext instance and configure it yourself. Spring Boot will do it for you. Have a look at JooqAutoConfiguration.java. But you probably *do* need to supply your DB configuration and have the DB driver on the classpath before Spring Boot will load the DSLContext. – jhyot Mar 05 '18 at 18:25
  • @jhyot yes you are right. i updated my answer. thank you – Jose Martinez Mar 05 '18 at 18:30
  • Thank you for your help. I added properties and jooqConfiguration class, but got another error message. I updated the question. – croul Mar 06 '18 at 02:22
  • Did you follow step 4 in the link I provided? – Jose Martinez Mar 06 '18 at 03:44
  • 1
    @JoseMartinez I re-installed JDK then problem fixed. Do not know the reason actually. Thank you . – croul Mar 06 '18 at 06:33
0

Just exclude R2dbc

@SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })
0

I've reproduced the same issue as:

Caused by: java.lang.ClassNotFoundException: org.jooq.DSLContext
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
    ... 11 common frames omitted

And it was fixed after adding in POM:

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

even without the usage of:

@SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })

and any additional configurations.

invzbl3
  • 5,872
  • 9
  • 36
  • 76