【问题标题】:Spring boot fails to load DataSource using PostgreSQL driverSpring boot 无法使用 PostgreSQL 驱动程序加载 DataSource
【发布时间】:2014-06-10 20:01:04
【问题描述】:

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

我搜索并尝试了以下解决方案: Spring Boot jdbc datasource autoconfiguration fails on standalone tomcat Spring Boot / Spring Data import.sql doesn't run 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'
}

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

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

我找不到哪里做错了:-(

【问题讨论】:

    标签: postgresql spring-data-jpa spring-jdbc spring-boot


    【解决方案1】:

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

    【讨论】:

    • 当然。复制粘贴错字!问题已更新。谢谢!
    • 什么错别字!好的,数据源而不是数据库 sigh
    【解决方案2】:
    spring.datasource.driver-class-name=org.postgresql.Driver
    

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

    【讨论】:

      【解决方案3】:

      如果依赖项未刷新,请尝试重新导入您的 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"
      

      【讨论】:

      【解决方案4】:

      我的build.gradle有这个:

      实现'org.postgresql:postgresql:版本号'

      但是,无论我做什么,jar 都没有下载。

      作为一种解决方法,我从here(点击Jar(...))下载了jar,并手动将其添加到我的Java Build Path - Libraries 中。 不理想,但这是唯一能在 2 分钟内起作用的东西!

      【讨论】:

        猜你喜欢
        • 2018-11-13
        • 2018-01-31
        • 2014-10-14
        • 2020-01-29
        • 2020-09-17
        • 1970-01-01
        • 2015-01-27
        • 2023-03-23
        相关资源
        最近更新 更多