将主题名称的数组列表传递给@KafkaListener

nit*_*nki 2 kotlin spring-boot spring-kafka

我试图按照https://github.com/spring-projects/spring-kafka/issues/361将主题名称从 .yml 文件传递​​到 @kafkalistener。但编译器抛出以下错误

Type mismatch.
Required:
Array<String>
Found:
String
 Unresolved reference: spring
Run Code Online (Sandbox Code Playgroud)

下面是接收者代码

@Component
class Receiver {

    companion object {
        private val LOGGER = LoggerFactory.getLogger(Receiver::class.java)
    }

    @Autowired
    private val taskExecutor: TaskExecutor? = null

    @Autowired
    private val applicationContext: ApplicationContext? = null

    @KafkaListener(topics = "#{'${spring.kafka.topics}'.split(',')}")
    fun receive(@Header(KafkaHeaders.RECEIVED_TOPIC) topic: String) {

    }    
}
Run Code Online (Sandbox Code Playgroud)

下面是我的 build.gradle 文件

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

plugins {
    id("org.springframework.boot") version "2.1.7.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
    kotlin("jvm") version "1.2.71"
    kotlin("plugin.spring") version "1.2.71"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.springframework.kafka:spring-kafka")
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

Art*_*lan 6

这对我有用:

@KafkaListener(topics = ["#{'\${test.topics}'.split(',')}"])
Run Code Online (Sandbox Code Playgroud)

请注意我如何将 Kotlin 语法应用于注释属性值。另请记住,这$是 Kotlin 中的模板特定运算符,因此我们需要对其进行转义,使其成为普通符号,以便进一步解析属性占位符。