Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin multiple classes in spring @Import

I'm using kotlin. I have two spring classes, com.example.SpringConfigA and com.example.SpringConfigB. I am trying to import them into a com.example.SpringConfigParent, but none of the following works:

Try 1, error: This annotation is not repeatable

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

Try 2, error: Type mismatch: inferred type is () -> ??? but KClass<*> was expected

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

Try 3, error: Only 'const val' can be used in constant expressions

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

What is the proper syntax in Kotlin for this?

EDIT: As @jaquelinep suggested, I forgot to add ::class, tries with that:

Try 1, error: This annotation is not repeatable

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

Try 2, error: Type mismatch: inferred type is () -> KClass<SpringConfigA> but KClass<*> was expected

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

Try 3, error: 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 {}    
like image 245
levant pied Avatar asked Nov 02 '25 04:11

levant pied


1 Answers

The syntax for multiple imports with one annotation is the following:

@Import(value = [Config1::class, Config2::class])
like image 81
Eamon Scullion Avatar answered Nov 04 '25 00:11

Eamon Scullion