Spring Boot 使用JOOQ的Sping Boot 收到消息“需要类型为'org.jooq.DSLContext'的bean,但找不到”

kx7yvsdv  于 2022-12-23  发布在  Spring
关注(0)|答案(4)|浏览(233)

我用springboot和jooq创建了一个简单的项目,添加了依赖项"spring-boot-starter-jooq"。当我编译时,应用程序无法启动。

    • 下面是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>
    • 这是我要注入DSLContext的位置:**
@Service
public class GameDao {
    private final DSLContext create;
    @Autowired
    public GameDao(DSLContext dslContext) {
        this.create = dslContext;
    }
}
    • 错误消息:**
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.

有没有人帮忙?
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.我添加了jooqConfiguration类,现在又收到了一条错误消息:

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'
w41d8nur

w41d8nur1#

人们在使用Spring Boot 2.4或2.5 with jOOQ 3.15+时可能会遇到以下消息:
没有类型为"org. jooq. DSLContext"的合格Bean可用:应至少有1个Bean符合自动连接候选项的条件。
......是因为jOOQ 3.15有一个新的R2DBC依赖项,而Spring Boot 2.4/2.5还没有为同时使用JDBC和R2DBC的第三方做好准备,另请参见How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0
大多数用户都不使用R2DBC,所以您可以排除R2dbcAutoConfiguration(注意拼写错误,它 * 不是 * R2dbcDataAutoConfiguration):

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

h7appiyu2#

您需要示例化DSLContext
首先添加属性:

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

编辑:根据jhyot的评论...添加配置应该足够好了,因为DSLContext将由JooqAutoConfiguration为您创建。
可选:添加配置以创建DSLContext Bean。以下是一个示例。

@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;
    }

}

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

62lalag4

62lalag43#

仅排除R2dbc

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

nwlls2ji4#

我复制了同样的问题:

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

添加POM后修复:

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

即使不使用:

@SpringBootApplication(exclude = { R2dbcAutoConfiguration.class })

以及任何附加的配置。

相关问题