簡體   English   中英

Spring Boot連接到Heroku上的Postgres數據庫

[英]Spring Boot connect to Postgres database on Heroku

我一直在研究部署在Heroku上的Spring Boot應用程序,但是偶然發現了一個似乎找不到解決方案的錯誤。

我試圖按照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

這是我正在使用的配置文件:

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

和DatabaseConfig類:

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

誰能指出我正確的方向。 我究竟做錯了什么?

我遇到了同樣的問題,並設法解決了這個問題。 該問題並非特定於Heroku,因為可以通過在本地運行該應用程序以及使用相同配置來復制該問題。

根據堆棧跟蹤很明顯,在類路徑中未找到數據源。 根據在這里找到的Spring Boot文檔,您可以使用spring-boot-starter-jdbcspring-boot-starter-data-jpa自動獲取tomcat-jdbc ,它似乎是Spring Boot中的首選。

我將以下依賴項添加到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