Nat*_*son 5 eclipse postgresql jdbc spring-boot
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]
Run Code Online (Sandbox Code Playgroud)
最终抛出错误:
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
Run Code Online (Sandbox Code Playgroud)
我已经使用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将数据源自动连接到我的 JDBC 实现:
@Component
public class JDBCRegionNameDAO implements RegionNameDAO {
private JdbcTemplate jdbcTemplate;
@Autowired
public JDBCRegionNameDAO (DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}
Run Code Online (Sandbox Code Playgroud)
我的 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
Run Code Online (Sandbox Code Playgroud)
我的 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')
}
Run Code Online (Sandbox Code Playgroud)
问题在于compile('org.springframework.boot:spring-boot-starter-jdbc')依赖性。将其更改为以下内容为我解决了问题:compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'
可能jdbc不再支持该包或其依赖项之一。`
添加commons-dbcp2为依赖项对我有用。
...
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19575 次 |
| 最近记录: |