简体   繁体   English

Spring Boot连接到Heroku上的Postgres数据库

[英]Spring Boot connect to Postgres database on Heroku

I've been playing around with a Spring Boot app deployed on Heroku but I've stumbled upon an error that I can't seem to find a solution. 我一直在研究部署在Heroku上的Spring Boot应用程序,但是偶然发现了一个似乎找不到解决方案的错误。

I'm trying to connect to a Postgres database following the Heroku tutorial ( link ) but I get this error over and over again: 我试图按照Heroku教程( link )连接到Postgres数据库,但一次又一次出现此错误:

Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [javax.sql.DataSource]:
Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found

Here's the config file I'm using: 这是我正在使用的配置文件:

spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.removeAbandoned=true

And the DatabaseConfig class: 和DatabaseConfig类:

@Configuration
public class DatabaseConfig {
    @Bean @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create()
                .build();
    }
}

Can anyone point me in the right direction. 谁能指出我正确的方向。 What am I doing wrong? 我究竟做错了什么?

I encountered this same exact issue and managed to solve it. 我遇到了同样的问题,并设法解决了这个问题。 The issue is not specific to Heroku, because it can be reproduced by running the app locally as well using the same configuration. 该问题并非特定于Heroku,因为可以通过在本地运行该应用程序以及使用相同配置来复制该问题。

According to the stacktrace it is clear that a DataSource has not been found in the class path. 根据堆栈跟踪很明显,在类路径中未找到数据源。 According to Spring Boot documentation, found here , you can either use spring-boot-starter-jdbc or spring-boot-starter-data-jpa to automatically get tomcat-jdbc , which appears to be the preferred one in Spring Boot. 根据在这里找到的Spring Boot文档,您可以使用spring-boot-starter-jdbcspring-boot-starter-data-jpa自动获取tomcat-jdbc ,它似乎是Spring Boot中的首选。

I added the following dependency to pom.xml, which solved the problem: 我将以下依赖项添加到pom.xml中,从而解决了该问题:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM