Spring boot无法使用PostgreSQL驱动加载DataSource

问题描述 投票:0回答:8

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

我已经搜索和搜索并尝试过以下解决方案: 独立 tomcat 上的 Spring Boot jdbc 数据源自动配置失败 Spring Boot / Spring Data import.sql 不运行 Spring-Boot-1.0.0.RC1

他们都建议让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

我的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'
}

应用程序.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

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

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

postgresql spring-data-jpa spring-jdbc spring-boot
8个回答
48
投票

这是从哪里来的:

database.driverClassName=org.postgresql.Driver
?你不是说
spring.datasource.driverClassName
吗?


15
投票
spring.datasource.driver-class-name=org.postgresql.Driver

如果您最近刚刚将 postgres 添加到依赖项中,请确保重新加载您的项目。


5
投票

如果依赖项没有刷新,请尝试重新导入您的 mvn 项目,同时将以下内容添加到您的

pom.xml

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

或者给你的

build.gradle

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

1
投票

我有一个类似的问题,这需要我检查我的 pom.xml 并且我已经离开了 H2 部门,将其替换为

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

似乎已经为我修好了。


0
投票

我有这个

build.gradle

实现'org.postgresql:postgresql:version-number'

但是,无论我做什么,jar 都不会下载。

作为变通方法,我从here(点击

Jar(...)
)下载了jar,并手动将其添加到我的Java构建路径-库中。 不理想,但这是唯一在 2 分钟内有效的方法!


0
投票

我刚看过这个帖子,但它确实帮助了我, 对我来说,问题也出在错误配置的 pom.xml 中, 我只需要添加 JPA:

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

希望有帮助!


0
投票

我在项目迁移到Java 17和gradle 7.5时遇到过这个问题

我已按照以下步骤解决问题。

gradle 导入升级为:

implementation 'org.postgresql:postgresql:42.2.27'

然后清理构建项目:

./gradlew clean build
然后
./gradlew bootRun

解决了问题。


0
投票

对于在寻找普通 JDBC 解决方案时登陆这里的任何人:

使用

jdbc:postgresql://localhost:5432/myDatabase?currentSchema=mySchema

使用 Hibernate 作为 JPA 提供程序时,以下工作:

spring.jpa.properties.hibernate.default_schema=mySchema

© www.soinside.com 2019 - 2024. All rights reserved.