levant pied
levant pied

Reputation: 4501

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 {}    

Upvotes: 4

Views: 4331

Answers (2)

Jaqueline Schweigert
Jaqueline Schweigert

Reputation: 83

You are missing the .class at the end of the class name:

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

I updated the answer, thanks to eamon-scullion

Upvotes: 0

Eamon Scullion
Eamon Scullion

Reputation: 1384

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

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

Upvotes: 17

Related Questions