我正在尝试遵循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"
}
}
我在这里想念什么?
答案 0 :(得分:2)
这对我有用:
@KafkaListener(topics = ["#{'\${test.topics}'.split(',')}"])
请注意我如何将Kotlin语法应用于注释属性值。
另外请记住,$
是Kotlin中特定于模板的运算符,因此我们需要转义使其成为进一步的属性占位符解析的简单符号。