2016-07-31 75 views
0

我一直在玩Heroku上部署的Spring Boot應用程序,但我偶然發現了一個我似乎無法找到解決方案的錯誤。春季啓動連接到Heroku上的Postgres數據庫

我試圖連接到以下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(); 
    } 
} 

任何人都可以點我在日正確的方向。我究竟做錯了什麼?

+0

嗨,你有沒有正確添加Maven的依賴關係爲PostgreSQL的JDBC驅動程序(pom.xml的)? ' org.postgresql PostgreSQL的 9.4-1201-jdbc4 ' –

+0

是 - 我也有Maven的依賴增加 – Alin

+0

你創建和綁定使用Heroku的命令行PostgreSQL的服務? $ heroku addons:add heroku-postgresql:hobby-dev –

回答

1

我遇到了同樣的確切問題,並設法解決它。這個問題並不特定於Heroku,因爲它可以通過在本地運行應用程序以及使用相同的配置進行再現。

根據堆棧跟蹤很清楚在類路徑中找不到DataSource。根據春季啓動文件,發現here,您可以使用彈簧引導啓動-JDBC彈簧引導起動數據JPA自動獲取的tomcat-JDBC,這似乎是最好的一個在春季啓動。

添加以下依賴性的pom.xml,它解決了這個問題:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-jdbc</artifactId> 
</dependency> 
相關問題