gpt4 book ai didi

spring-boot - 将主题名称数组列表传递给@KafkaListener

转载 作者:行者123 更新时间:2023-12-02 16:52:22 27 4
gpt4 key购买 nike

我正在尝试关注 https://github.com/spring-projects/spring-kafka/issues/361将主题名称从 .yml 文件传递​​给@kafkalistener。但是编译器抛出以下错误

Type mismatch.
Required:
Array<String>
Found:
String
Unresolved reference: spring

接收代码如下

@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) {

}
}

下面是我的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"
}
}

我在这里错过了什么?

最佳答案

这对我有用:

@KafkaListener(topics = ["#{'\${test.topics}'.split(',')}"])

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

关于spring-boot - 将主题名称数组列表传递给@KafkaListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57953246/

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