I am unable to properly inject an #Value application property in my Kotlin Spring Boot application. The property as defined in my application.yml file, and subsequently referenced in an additional-spring-configuration-metadata.json file (under resources -> META-INF), is not properly being added to the bean expression context. Using IntelliJ version 2020.2.1, when I hover over the property, I see a Cannot resolve configuration property error. Attempting to run the application (with the configuration property value construction-injected into a class) leads to a Unsatisfied dependency expressed through constructor parameter error.
build.gradle.kts
plugins {
id("org.springframework.boot") version "2.3.3.RELEASE"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
}
group = "com.myProject"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
extra["springCloudVersion"] = "Hoxton.SR8"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("io.projectreactor:reactor-test")
}
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("com.google.cloud.tools:appengine-gradle-plugin:2.2.0")
}
}
apply(plugin = "com.google.cloud.tools.appengine")
configure<com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension> {
deploy {
projectId = "my-cloud-project"
version = "GCLOUD_CONFIG"
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
Error Message
Spring error modal
additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "otherApi.baseUrl",
"type": "java.lang.String",
"description": "Description for otherApi.baseUrl."
}
]
}
I've added annotation processing dependencies, invalidated caches and restarted, and played around with Kotlin specific annotation processors (kapt). I've also followed the instructions here: https://www.jetbrains.com/help/idea/annotation-processors-support.html
What am I missing? Any and all help would be appreciated. Thanks!
you need to declare the below as as annoatationProcessor
implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
to
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
If your application.yml (or application.properties) looks like this:
spring:
datasource:
username: postgres
password: postgres
url: jdbc:postgresql://localhost:5433/company
driver-class-name: org.postgresql.Driver
then try to rewrite each property to full-name format for every property:
spring.datasource.username: postgres
spring.datasource.password: postgres
spring.datasource.url: jdbc:postgresql://localhost:5433/company
spring.datasource.driver-class-name: org.postgresql.Driver
Related
I am using application.yml file to configure data source. When I run the jar file, I get the exception.
It works fine if I run project in IntelliJ (run button)
Command I use to build and run jar
./gradlew build -x check
java -jar build/libs/UrlShortener-0.0.1-SNAPSHOT-plain.jar
application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5332/dbName
username: username
password: password
driverClassName: org.postgresql.Driver
Exception
05:13:50.339 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.2"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
kotlin("plugin.jpa") version "1.7.22"
}
group = "url.shortener"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.postgresql:postgresql")
val kotlinxHtmlVersion = "0.8.0"
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:$kotlinxHtmlVersion")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
But if I configure through kotlin, it works
#Configuration
class Config {
#Bean
fun getDataSource(): DataSource {
val dataSourceBuilder = DataSourceBuilder.create()
dataSourceBuilder.driverClassName("org.postgresql.Driver")
dataSourceBuilder.url("jdbc:postgresql://localhost:5332/dbName")
dataSourceBuilder.username("username")
dataSourceBuilder.password("password")
return dataSourceBuilder.build()
}
}
taking out your customisation for jar seems to resolve the issue.
//tasks.withType<Jar> {
// manifest {
// attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
// }
// duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// from({
// configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
// })
//
//}
I created a project with spring initializr with kotlin and gradle to study hexagonal architecture in microservices. I'm using IntelliJ with modules to dividing the code but the spring-jpa dependency doesn't work in module (or subproject).
The start build.gradle.kts is:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.9-SNAPSHOT"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.32"
kotlin("plugin.spring") version "1.5.32"
kotlin("plugin.jpa") version "1.5.32"
}
group = "com.donadon.studyhexagonal"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-redis-reactive")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I move the repositories and dependencies to subprojects method e I put some plugins together but the follow error happened:
subprojects {
apply {
plugin("kotlin")
plugin("kotlin-jpa")
plugin("io.spring.dependency-management")
}
repositoreis { ... }
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
...
}
}
The error:
Configuration with name 'implementation' not found.
at Program.execute(Unknown Source)
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'implementation' not found.
at Build_gradle$2$2.invoke(build.gradle.kts:29)
How I make for dependencies work to all subprojects?
I read the gradle doc and others questions here but nothing helped me.
Thanks for while.
EDIT
I created a module with name database and when I try to use #Entity, it is not found but if I use in some class in main src, it founds the annotation.
Couple things:
Some gradle configurations cannot be shared using the subprojects method. This includes dependencies. Check out this StackOverflow question for information on how to share dependency versions.
According to gradle, reusing logic through subprojects is discouraged: Another, discouraged, way to share build logic between subproject is cross project configuration via the subprojects {} and allprojects {} DSL constructs. (Link)
I'm new at spring boot with kotlin.
I have error when application start up.
I had been seeking various solutions in the other answers in Stackoverflow.
But no answers gave me no solutions.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
My application.properties configuration is following.
spring.datasource.url=jdbc:mysql://localhost/db_name
spring.datasource.username=db_user
spring.datasource.password=db_pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
And build.gradle.kts is following.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.2.0.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
war
kotlin("jvm") version "1.3.50"
kotlin("plugin.spring") version "1.3.50"
kotlin("plugin.jpa") version "1.3.50"
}
group = "jp.co.blowfish.springboot"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("mysql:mysql-connector-java")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I have no idea what is wrong.
It is missing the port of Mysql by default is 3306
spring.datasource.url=jdbc:mysql://localhost:3306/db_name`
or application.properties is in resources folder.
I have an issue when trying to add a few dependencies to my kotlin spring project. I used the spring boot initializer to get a basic project running.
My problem: If I uncomment jackson or either Koin dependency then my build fails with the mentioned in the titile
Here is the build.gradle.kts file :
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.2.0.M4"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/snapshot") }
maven { url = uri("https://repo.spring.io/milestone") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
// jackson
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
// Koin
//implementation("org.koin:koin-core:2.0.1")
// Koin Test
//implementation("org.koin:koin-test:2.0.1")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I Have already tried all the solutions from this previous question someone else asked: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
I already had the KotlinCompile jvmTarget option in there and it has been set in my build settings as well. Any help is appreciated thanks!
A lot of the answers to this are android specific - using kotlin to write a spring boot app isn't that well documented or supported but still awesome. This setting in intelij cleared this error for me - hope it helps someone, my kotlin compiler had a target set to 1.6:
I am working on a reactive spring boot api server. I first wanted to use MVC pattern, but I thought reactor would be a good idea.
So I have deleted all spring dependencies on MVC (I believe).
But spring keeps complaining that I can't use #EnableWebMvc along #EnableWebFlux.
Following is my error log
Caused by: java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via #EnableWebMvc and #EnableWebFlux, in the same application.
What possibly could be the problem? I sure did updated my dependencies.
And following is my build.gradle.kts file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "gradle.plugin.com.palantir.gradle.docker", name = "gradle-docker")
}
}
plugins {
kotlin("plugin.jpa") version "1.3.40"
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
id("com.palantir.docker") version "0.22.1"
kotlin("jvm") version "1.3.40"
kotlin("plugin.spring") version "1.3.40"
}
group = "com.mycompany"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven(url = "https://repo.spring.io/snapshot")
maven(url = "https://repo.spring.io/milestone")
}
extra["springCloudVersion"] = "Greenwich.SR1"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-batch")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
//reactor
implementation("io.projectreactor:reactor-core")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT")
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M1")
implementation("io.r2dbc:r2dbc-spi:1.0.0.M5")
implementation("io.r2dbc:r2dbc-postgresql:1.0.0.M6")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.batch:spring-batch-test")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
mavenBom("io.projectreactor:reactor-bom:Bismuth-RELEASE")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
task<Copy>("unpack") {
dependsOn(tasks.getByName("bootJar"))
from(zipTree(tasks.getByName("bootJar").outputs.files.singleFile))
into("build/dependency")
}
I figured it out by deleting each dependencies I have.
The problem was
implementation("org.springframework.boot:spring-boot-starter-web")
After deleting this, It worked. Maybe above dependency has it's own dependencies on WebMvc but I'm not sure.