Почему @ConstructorBinding не работает даже при использовании @EnableConfigurationProperties или @ConfigurationPropertiesScan? - PullRequest
0 голосов
/ 12 июля 2020

У меня проблема с тем, чтобы @ConstructorBinding работал в моем приложении. Я также использую Bean Definition DSL. Вот приложение:

@SpringBootApplication
@EnableConfigurationProperties(CallMeProperties::class)
@ConfigurationPropertiesScan(basePackages = ["pl.app"])
class CallmeApp

fun main(){
    runApplication<CallmeApp> {
        addInitializers(appBeans())
    }
}

fun appBeans() = org.springframework.context.support.beans {
    bean<CallMeProperties>()

    bean {
        TestRouter(ref()).testRouter()
    }
}

@ConstructorBinding
@ConfigurationProperties(prefix = "callme")
data class CallMeProperties(
       var property1: String,
       var property2: String
)

Вот application.yml:

callme:
  property1: "Test1"
  property2: "Test2cfg"

и build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.3.1.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
    kotlin("kapt") version "1.3.72"
}

group = "pl.app.callme"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

extra["springCloudVersion"] = "Hoxton.SR6"

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.cloud:spring-cloud-starter-config")
    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")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
    runtimeOnly("io.micrometer:micrometer-registry-prometheus")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

Я также пробовал настроить только @EnableConfigurationProperties(CallMeProperties::class) и только '@ConfigurationPropertiesScan (basePackages = ["pl.app"])', но это тоже не работает.

Я получаю сообщение об исключении: @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type

Полное исключение:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pl.app.callme.CallMeProperties#0': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type pl.app.callme.CallMeProperties
    at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:66) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:291) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:175) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:62) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at pl.app.callme.CallmeAppKt.main(CallmeApp.kt:36) ~[main/:na]
    at pl.app.callme.CallmeAppKt.main(CallmeApp.kt) ~[main/:na]

Что я делаю не так?

1 Ответ

0 голосов
/ 12 июля 2020

Таким образом, настоящая проблема заключается в вводящем в заблуждение сообщении об исключении.

Удаление

bean<CallmeProperties>()

из определения beans решает проблему.

...