I am trying to create a DataSource as is suggested in some Spring examples online this way:
@Configuration
public class RepositoryConfiguration {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
I have an application.yml file that looks like this:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:54320/mydb
username: postgres
password: somepassword
When I run the debugger and evaluate DataSourceBuilder.create().build();
the values don't seem to be set. Any idea what could be missing?
I am using version 2.2.4 of Spring boot
This is my build.gradle
file
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"]
}
At the moment the app is failing to boot:
***************************
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
is processed by ConfigurationPropertiesBindingPostProcessor
after the bean creation.
So don't worry, your properties will be set later during app's initialization. You can set some breakpoints in that postprocessor or even in HikariConfig
( HikariDataSource
is used by default in Spring Boot 2, it can be something else) and see the process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.