春季启动连接到Heroku上的Postgres数据库
问题描述:
我一直在玩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();
}
}
任何人都可以点我在日正确的方向。我究竟做错了什么?
答
我遇到了同样的确切问题,并设法解决它。这个问题并不特定于Heroku,因为它可以通过在本地运行应用程序以及使用相同的配置进行再现。
根据堆栈跟踪很清楚在类路径中找不到DataSource。根据春季启动文件,发现here,您可以使用弹簧引导启动-JDBC或弹簧引导起动数据JPA自动获取的tomcat-JDBC,这似乎是最好的一个在春季启动。
添加以下依赖性的pom.xml,它解决了这个问题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
嗨,你有没有正确添加Maven的依赖关系为PostgreSQL的JDBC驱动程序(pom.xml的)? ' org.postgresql PostgreSQL的 9.4-1201-jdbc4 ' –
是 - 我也有Maven的依赖增加 – Alin
你创建和绑定使用Heroku的命令行PostgreSQL的服务? $ heroku addons:add heroku-postgresql:hobby-dev –