我正在使用 maven 构建一个 Springboot(版本 2.0.4)项目来使用 PostgreSQL 数据库。我想利用 Springboot 的数据源自动配置功能,但它给了我以下错误:
Field dataSource in com.praveen.demo.MyController required a bean of type 'javax.sql.DataSource' that could not be found.
Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required classes 'javax.transaction.TransactionManager', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
在我的 pom.xml 中,我依赖于 postgresql 和 HikariCP,如下所示:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
我还在我的 application.properties 文件中
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb?user=myself&password=mypassword
在我的具有 @RestController 注释的 Java 文件中,我注入(inject) DataSource,如下所示:
@Autowired
private DataSource dataSource;
我正在关注以下文章:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html 我不想使用 JPA。我相信文章并不是建议使用 JPA 进行自动配置工作。 我期望 Spring boot 应该自动配置应用程序的数据源,因为我已经声明了依赖项并提供了数据库 URL。我仍然在启动应用程序时遇到错误(如上面提到的)。
Edit-1:我正在关注以下文章: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html
编辑 2:完成 POM:
<?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.praveen</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
尽管如此,我仍然对 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html 提供的文档感到困惑并期望如果我明确声明对 HikariCP 和 Postgresql 的依赖,则不需要使用 JPA 进行自动配置工作。暂时关闭它。
最佳答案
如果你不想使用 JPA,你的 pom 就可以。
为Datasource定义@Bean
,在application.properties spring.datasource.type=com.zaxxer.hikari.HikariDataSource
中配置或添加属性:
@Configuration
public class DbConfig {
@Bean
@ConfigurationProperties("spring.datasource")
public HikariDataSource dataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
}
还将您的属性更改为以下内容:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username-myself
spring.datasource.password= mypassword
注意:JPA 会为您配置一切,因此如果不使用 JPA,那么您需要通过告诉 Hikari 来配置数据源。
根据引用文件29.1.2 Connection to a Production Database
If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”, you automatically get a dependency to HikariCP.
You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type property. This is especially important if you run your application in a Tomcat container, as tomcat-jdbc is provided by default.
[Tip] Additional connection pools can always be configured manually. If you define your own DataSource bean, auto-configuration does not occur.
关于java - PostgreSql 失败的 Spring Boot 数据源自动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52454259/