eclipse java.lang.IllegalStateException:找不到支持的数据源类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46889943/
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
java.lang.IllegalStateException: No supported DataSource type found
提问by Nate Anderson
Spring boot can't create my Postgres datasource. The error is starting here:
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]
Ultimately throwing error:
最终抛出错误:
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
I've built Spring Apps using xml configuration and Maven, but Spring Boot and Gradle are new to me. I'm used to @Autowire
pulling the datasource from the config file. Based on some SO answers, I added a database config class:
我已经使用 xml 配置和 Maven 构建了 Spring Apps,但 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();
}
}
I'm trying to autowire the datasource to my JDBC implementation:
我正在尝试将数据源自动连接到我的 JDBC 实现:
@Component
public class JDBCRegionNameDAO implements RegionNameDAO {
private JdbcTemplate jdbcTemplate;
@Autowired
public JDBCRegionNameDAO (DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}
My application.properties, I've checked my postgres credentials, and ensured the service is running:
我的 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
My build.gradle, from what I've looked at, it seems that I have everything here that I need, but the error being thrown above suggests I'm missing something:
我的 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')
}
回答by Nate Anderson
The issue was with the compile('org.springframework.boot:spring-boot-starter-jdbc')
dependency. Changing this to the following solved the issue for me:compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'
问题在于compile('org.springframework.boot:spring-boot-starter-jdbc')
依赖性。将其更改为以下内容为我解决了这个问题:compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'
Possibly the jdbc
package or one of its dependencies is no longer supported.
`
可能jdbc
不再支持该包或其依赖项之一。`