gpt4 book ai didi

java - 无法使用 Spring 以编程方式创建 Postgres 数据源

转载 作者:行者123 更新时间:2023-12-01 17:48:01 25 4
gpt4 key购买 nike

我正在尝试按照一些在线 Spring 示例中的建议创建一个数据源:

    @Configuration
public class RepositoryConfiguration {

@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

}

我有一个如下所示的 application.yml 文件:

spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:54320/mydb
username: postgres
password: somepassword

当我运行调试器并评估 DataSourceBuilder.create().build(); 时,值似乎未设置。知道可能缺少什么吗?

enter image description here

我使用的是Spring boot 2.2.4版本

这是我的build.gradle 文件

buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.postgresql:postgresql:42.2.11"
}
}

plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'org.flywaydb.flyway' version '6.3.0'
id 'nu.studer.jooq' version '4.1'
}

group 'com.solution.asclepius'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {

annotationProcessor 'org.projectlombok:lombok:1.18.8'

implementation 'org.jooq:jooq'
implementation 'org.jooq:jooq-codegen'
jooqRuntime 'org.postgresql:postgresql:42.2.11'
implementation 'org.projectlombok:lombok:1.18.8'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'io.vavr:vavr:0.10.2'

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.15.0'

}

jooq {
sample(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:54320/asclepiusdb'
user = 'postgres'
password = 'somepassword'
}
generator {
database() {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
includes = '.*'
}
target {
packageName = 'com.solution.asclepius'
directory = 'build/generated/java'
}
}
}
}

tasks.generateSampleJooqSchemaSource.with {
def out = new ByteArrayOutputStream()
javaExecSpec = { JavaExecSpec s ->
s.standardOutput = out
s.errorOutput = out
s.ignoreExitValue = true
s.jvmArgs '-Xmx512M'
}
execResultHandler = { ExecResult r ->
if (r.exitValue != 0) {
throw new RuntimeException('jOOQ source code generation failed:\n\n' + out.toString())
}
}
}

flyway {
url = 'jdbc:postgresql://localhost:54320/asclepiusdb'
user = 'postgres'
password = 'somepassword'
schemas = ['public']
locations = ["filesystem:$project.projectDir/src/main/resources/db/migration"]
}

目前应用程序无法启动:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:

Property: spring.datasource.driver-class-name
Value: org.postgresql.Driver
Origin: class path resource [application.yml]:4:24
Reason: Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader

Action:

Update your application's configuration

最佳答案

@ConfigurationProperties 在 bean 创建后由 ConfigurationPropertiesBindingPostProcessor 处理。

所以不用担心,您的属性将在应用程序初始化期间设置。您可以在该后处理器中设置一些断点,甚至在 HikariConfig 中设置一些断点(Spring Boot 2 中默认使用 HikariDataSource,它可以是其他东西)并查看该过程。

关于java - 无法使用 Spring 以编程方式创建 Postgres 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60833827/

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