Spring boot 无法使用 PostgreSQL 驱动程序加载数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23287462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:41:01  来源:igfitidea点击:

Spring boot fails to load DataSource using PostgreSQL driver

postgresqlspring-data-jpaspring-jdbcspring-boot

提问by mamruoc

I have successfully developed a prototype using Spring Boot 1.0.2.RELEASE (was 1.0.1.RELEASE until today).

我已经成功地使用 Spring Boot 1.0.2.RELEASE 开发了一个原型(直到今天是 1.0.1.RELEASE)。

I have searched and searched and tried solutions like: Spring Boot jdbc datasource autoconfiguration fails on standalone tomcatSpring Boot / Spring Data import.sql doesn't run Spring-Boot-1.0.0.RC1

我已经搜索和搜索并尝试过类似的解决方案: Spring Boot jdbc datasource autoconfiguration failed on standalone tomcat Spring Boot / Spring Data import.sql 不运行 Spring-Boot-1.0.0.RC1

They all suggests to let Spring Boot do the job. When using H2, everything works, but when I try to switch to PostgreSQL, i get:

他们都建议让 Spring Boot 来完成这项工作。使用 H2 时,一切正常,但是当我尝试切换到 PostgreSQL 时,我得到:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined

My build.gradle is as follow:

我的 build.gradle 如下:

loadConfiguration()

def loadConfiguration() {
def environment = hasProperty('env') ? env : 'dev'
project.ext.envrionment = environment
println "Environment is set to $environment"

def configFile = file('config.groovy')
def config = new ConfigSlurper("$environment").parse(configFile.toURL())
project.ext.config = config
}

buildscript {
ext {
    springBootVersion = '1.0.2.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-   plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'groovy'

war {
baseName = 'test'
version =  '0.0.1-SNAPSHOT'
}

configurations {
providedRuntime
}

repositories {
mavenCentral()
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
compile("postgresql:postgresql:9.1-901.jdbc4")
//compile("com.h2database:h2")

testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")

}

task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}

application.properties:

应用程序属性:

spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update

spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/cms
spring.datasource.username=cms
spring.datasource.password=NA

Removing application.properties and changing the dependency back to H2 and everything is OK.

删除 application.properties 并将依赖项改回 H2,一切正常。

I can't find where I am doing wrong :-(

我找不到我做错的地方:-(

回答by Dave Syer

Where did this come from: database.driverClassName=org.postgresql.Driver? Don't you mean spring.datasource.driverClassName?

这是从哪里来的:database.driverClassName=org.postgresql.Driver?你不是说spring.datasource.driverClassName吗?

回答by Pravin

some time try re-import your mvn project if dependencies are not refreshed , also add following to your pom.xml

如果依赖项没有刷新,一段时间尝试重新导入您的 mvn 项目,还将以下内容添加到您的 pom.xml

 <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
</dependency>

Or to your build.gradle

或者给你 build.gradle

implementation "postgresql:postgresql:9.1-901.jdbc4"