SpringBoot2 配置 JOOQ MariaDB:"required a bean of type 'javax.sql.DataSource' that could not be found"
SpringBoot2 Configuration JOOQ MariaDB: "required a bean of type 'javax.sql.DataSource' that could not be found"
这不是第一次被问了,我看了很多关于这个话题的文章,但我无法解决这个问题:
Field dataSource in de.foo.MariaDbConfig required a bean of type 'javax.sql.DataSource' that could not be found.
我所有的配置都和本文中的一样。代码生成与给定的 ddl sql 文件一起正常工作。数据库是 MariaDB。为什么这个错误一直出现?
配置
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>
我也这样定义了自己的DataSource,但是错误不断出现:
@Bean
public DataSource datasource() {
return DataSourceBuilder.create()
.driverClassName("org.mariadb.jdbc.Driver")
.url("jdbc:mariadb://localhost:3306/foo")
.username("root")
.password("foo123")
.build();
}
我希望你们中的一些人有想法。
所以 Simon Martinelli 通过在 Whosebug 中找到解决方案来帮助我。删除洞配置 class 后,我得到了这个“找不到 DSLContext”的错误。然后我发现了这个问题:
彻底解决了我的问题。很抱歉提出一个已经解决的问题,但没有指向 R2dbcAutoConfiguration 的方向。
所以,正如 Simon 所说,删除配置 class,并且正如这个问题的答案已经指出的那样,添加 @SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })
解决了问题。
这不是第一次被问了,我看了很多关于这个话题的文章,但我无法解决这个问题:
Field dataSource in de.foo.MariaDbConfig required a bean of type 'javax.sql.DataSource' that could not be found.
我所有的配置都和本文中的一样。代码生成与给定的 ddl sql 文件一起正常工作。数据库是 MariaDB。为什么这个错误一直出现?
配置
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>
我也这样定义了自己的DataSource,但是错误不断出现:
@Bean
public DataSource datasource() {
return DataSourceBuilder.create()
.driverClassName("org.mariadb.jdbc.Driver")
.url("jdbc:mariadb://localhost:3306/foo")
.username("root")
.password("foo123")
.build();
}
我希望你们中的一些人有想法。
所以 Simon Martinelli 通过在 Whosebug 中找到解决方案来帮助我。删除洞配置 class 后,我得到了这个“找不到 DSLContext”的错误。然后我发现了这个问题:
彻底解决了我的问题。很抱歉提出一个已经解决的问题,但没有指向 R2dbcAutoConfiguration 的方向。
所以,正如 Simon 所说,删除配置 class,并且正如这个问题的答案已经指出的那样,添加 @SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })
解决了问题。