【问题标题】:java.lang.IllegalStateException: No supported DataSource type foundjava.lang.IllegalStateException:找不到支持的数据源类型
【发布时间】:2018-04-04 00:51:57
【问题描述】:

Spring boot 无法创建我的 Postgres 数据源。错误从这里开始:

at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.getType(DataSourceBuilder.java:138) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]

最终抛出错误:

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

我使用 xml 配置和 Maven 构建了 Spring 应用程序,但 Spring Boot 和 Gradle 对我来说是新的。我习惯@Autowire 从配置文件中提取数据源。基于一些 SO 答案,我添加了一个数据库配置类:

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource({ "classpath:application.properties" })
public class DatabaseConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {

        return DataSourceBuilder.create().build();

    }

}

我正在尝试将数据源自动连接到我的 JDBC 实现:

@Component
public class JDBCRegionNameDAO implements RegionNameDAO {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public JDBCRegionNameDAO (DataSource dataSource)  {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

}

我的 application.properties,我检查了我的 postgres 凭据,并确保服务正在运行:

spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.url=jdbc:postgresql://localhost:5432/world_builder
spring.datasource.platform=postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

我的 build.gradle,从我所看到的来看,似乎我在这里拥有我需要的一切,但上面抛出的错误表明我错过了一些东西:

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

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'group.group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies { 

    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-web')

    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-devtools')

    compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'

    runtime('org.postgresql:postgresql')    

    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

【问题讨论】:

标签: eclipse postgresql spring-boot jdbc


【解决方案1】:

问题在于 compile('org.springframework.boot:spring-boot-starter-jdbc') 依赖项。将其更改为以下解决了我的问题:compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'

可能不再支持 jdbc 包或其依赖项之一。 `

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2013-07-31
    • 2017-08-22
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多