Home > Software engineering >  SpringBoot2 Configuration JOOQ MariaDB: "required a bean of type 'javax.sql.DataSource
SpringBoot2 Configuration JOOQ MariaDB: "required a bean of type 'javax.sql.DataSource

Time:09-17

it's not the first time this question was asked and i read a lot of article about this topic, but I can't solve this problem:

Field dataSource in de.foo.MariaDbConfig required a bean of type 'javax.sql.DataSource' that could not be found.

All my configurations are like in this articles. Code generation worked properly with a given ddl sql file. The database is a MariaDB. Why does this error keep appearing?

Config

import org.jooq.SQLDialect;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import javax.sql.DataSource;

@Configuration
public class MariaDbConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public DataSourceConnectionProvider connectionProvider() {

        return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource));
    }

    @Bean
    public DefaultDSLContext dslContext(final DefaultConfiguration defaultConfiguration) {

        return new DefaultDSLContext(defaultConfiguration);
    }

    @Bean
    public DefaultConfiguration defaultConfiguration(final DataSourceConnectionProvider connectionProvider) {

        DefaultConfiguration jooqConfiguration = new DefaultConfiguration();

        jooqConfiguration.set(connectionProvider);
        jooqConfiguration.setSQLDialect(SQLDialect.MARIADB);
        jooqConfiguration.set(new DefaultExecuteListenerProvider(new ExceptionTranslator()));

        return jooqConfiguration;
    }
}

application.yml

spring:
  application:
    name:                         '@project.artifactId@'
  datasource:
    driverClassName:              org.mariadb.jdbc.Driver
    url:                          jdbc:mariadb://localhost:3306/foo
    username:                     root
    password:                     foo123

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>de.foo</groupId>
    <artifactId>foobar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>foobarapp</name>
    <properties>
        <java.version>11</java.version>
        <testcontainers.version>1.15.3</testcontainers.version>
        <jooq.version>3.15.2</jooq.version>
    </properties>
    <dependencies>
        <!--WEB&IO-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <!-- openapi generator libs -->
        <!--
          The open-api maven plugin generates classes with swagger-annotation.
          Unfortunately, there is no feature toggle for it, therefore we have to live with generated stubs using swagger annotations
        -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.6.2</version>
        </dependency>
        <!-- Required for model classes generated by openapi-generator-maven-plugin because of bug https://github.com/OpenAPITools/openapi-generator/issues/2901
     The config tag <openApiNullable> is fully supported from 5.0.0 onwards -->
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.1</version>
        </dependency>
        <!-- Required by the generated API to add support for xml format-->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.12.5</version>
        </dependency>

        <!--DB-->
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta-extensions</artifactId>
            <version>${jooq.version}</version>
        </dependency>

        <!--TOOLS-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>


        <!--TEST-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>mariadb</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8</artifactId>
            <version>2.27.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.testcontainers</groupId>
                <artifactId>testcontainers-bom</artifactId>
                <version>${testcontainers.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!--API CODE GEN-->
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/foo.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>${project.groupId}.ui.api</apiPackage>
                            <modelPackage>${project.groupId}.ui.api.model</modelPackage>
                            <supportingFilesToGenerate>
                                ApiUtil.java
                            </supportingFilesToGenerate>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <delegatePattern>true</delegatePattern>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- Run this profile to generate DB entities from schema ddl file. -->
    <profiles>
        <profile>
            <id>jooq</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jooq</groupId>
                        <artifactId>jooq-codegen-maven</artifactId>
                        <version>${jooq.version}</version>
                        <executions>
                            <execution>
                                <id>generate-jooq-sources</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <generator>
                                        <generate>
                                            <relations>true</relations>
                                            <deprecated>false</deprecated>
                                            <pojos>false</pojos>
                                            <daos>false</daos>
                                            <pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
                                            <javaTimeTypes>true</javaTimeTypes>
                                        </generate>
                                        <database>
                                            <name>org.jooq.meta.extensions.ddl.DDLDatabase</name>
                                            <inputCatalog></inputCatalog>
                                            <inputSchema>PUBLIC</inputSchema>
                                            <outputSchemaToDefault>true</outputSchemaToDefault>
                                            <outputCatalogToDefault>true</outputCatalogToDefault>
                                            <properties>
                                                <property>
                                                    <key>sort</key>
                                                    <value>semantic</value>
                                                </property>
                                                <property>
                                                    <key>parseIgnoreComments</key>
                                                    <value>true</value>
                                                </property>
                                                <property>
                                                    <key>parseDialect</key>
                                                    <value>SQLSERVER</value>
                                                </property>
                                                <property>
                                                    <key>scripts</key>
                                                    <value>src/main/resources/foo.sql</value>
                                                </property>
                                            </properties>
                                        </database>
                                        <target>
                                            <clean>true</clean>
                                            <packageName>de.foo</packageName>
                                            <directory>src/main/java</directory>
                                        </target>
                                    </generator>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

I also defined my own DataSource like this but the error keeps coming up:

@Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
          .driverClassName("org.mariadb.jdbc.Driver")
          .url("jdbc:mariadb://localhost:3306/foo")
          .username("root")
          .password("foo123")
          .build(); 
    }

I hope some of you guys got an idea.

CodePudding user response:

So Simon Martinelli helped me by finding the solution inside stackoverflow. After removing the hole config class, i got this "DSLContext that could not be found" error. Then i found this question:

How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0

Which solved my problem completly. Sorry for asking a question that has already been solved, but nothing pointed into the direction of R2dbcAutoConfiguration.

So, as Simon already said, removing the config class and, as the answer in this question already pointed out, adding @SpringBootApplication(exclude = { R2dbcAutoConfiguration.class }) solved the problem.

  • Related