我创建了一个带有spring boot和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;
}
}
错误信息:
上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件中定义名为'gameDao'的bean时出错[/ Users / yangzhiwei / Downloads / BingoApi / target / classes / com / bingo / api /dao/GameDao.class]:通过构造函数参数0表示的不满意的依赖;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.jooq.DSLContext'的限定bean可用:预期至少有1个bean有资格作为autowire候选者。依赖注释:{}
/***************************
应用程序未能启动
/***************************
说明:com.bingo.api.dao.GameDao中构造函数的参数0需要一个无法找到的类型为'org.jooq.DSLContext'的bean。操作:考虑在配置中定义类型为'org.jooq.DSLContext'的bean。
有什么帮助吗?
更新:1.我在application.properties文件中添加了如下属性:
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'
你需要实例化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;
}
}