Spring PostgreSql 的引导数据源自动配置失败

Spring boot data source auto configuration for PostgreSql failing

我正在设置一个 Springboot(版本 2.0.4)项目,使用 maven build 来使用 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

在我的 Java 带有 @RestController 注释的文件中,我按如下方式注入数据源:

@Autowired
private DataSource dataSource;

我正在关注以下文章:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html 我不想使用 JPA。我相信文章并不建议使用 JPA 进行自动配置。 我期望 Spring 引导应该为应用程序自动配置数据源,因为我已经声明了依赖关系并提供了数据库 URL。我仍然在启动应用程序时遇到错误(如上文所述)。

Edit-1:我正在关注以下文章: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Edit-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 也可以。

为数据源定义@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 来配置 DataSource。

根据参考文档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.