首页 文章

java.lang.IllegalStateException:找不到支持的DataSource类型

提问于
浏览
1

Spring引导无法创建我的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

我've built Spring Apps using xml configuration and Maven, but Spring Boot and Gradle are new to me. I'曾经 @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')
}

1 回答

  • 1

    问题在于 compile('org.springframework.boot:spring-boot-starter-jdbc') 依赖关系 . 将此更改为以下内容为我解决了问题: compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'

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

相关问题