Spring中的Kotlin多个类@Import

6mw9ycah  于 2023-03-28  发布在  Spring
关注(0)|答案(2)|浏览(154)

我正在使用Kotlin。我有两个spring类,com.example.SpringConfigAcom.example.SpringConfigB。我试图将它们导入到com.example.SpringConfigParent中,但以下都不起作用:
尝试1,错误:This annotation is not repeatable

@Import(com.example.SpringConfigA)
@Import(com.example.SpringConfigB)
class SpringConfigParent {}

尝试2,错误:Type mismatch: inferred type is () -> ??? but KClass<*> was expected

@Import({com.example.SpringConfigA, com.example.SpringConfigB})
class SpringConfigParent {}

尝试3,错误:Only 'const val' can be used in constant expressions

@Import(arrayOf(com.example.SpringConfigA, com.example.SpringConfigB))
class SpringConfigParent {}

在Kotlin中,正确的语法是什么?

**编辑:**正如@jaquelinep建议的那样,我忘记添加::class,尝试使用它:

尝试1,错误:This annotation is not repeatable

@Import(com.example.SpringConfigA::class)
@Import(com.example.SpringConfigB::class)
class SpringConfigParent {}

尝试2,错误:Type mismatch: inferred type is () -> KClass<SpringConfigA> but KClass<*> was expected

@Import({com.example.SpringConfigA::class, com.example.SpringConfigB::class})
class SpringConfigParent {}

尝试3,错误:Type inference failed. Expected type mismatch: inferred type is Array<KClass<out Any>> but KClass<*> was expected

@Import(arrayOf(com.example.SpringConfigA::class, com.example.SpringConfigB::class))
class SpringConfigParent {}
odopli94

odopli941#

带有一个注解的多个导入的语法如下:

@Import(value = [Config1::class, Config2::class])
w1jd8yoj

w1jd8yoj2#

您在类名末尾缺少.class:

@Import({com.example.SpringConfigA::class, com.example.SpringConfigB::class})
class SpringConfigParent {}

我更新了答案感谢eamon-scullion

相关问题