gpt4 book ai didi

eclipse - java.lang.IllegalStateException : No supported DataSource type found 错误

转载 作者:行者123 更新时间:2023-11-29 11:46:52 30 4
gpt4 key购买 nike

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')
}

最佳答案

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

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

关于eclipse - java.lang.IllegalStateException : No supported DataSource type found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889943/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com