How to make kotlin’s context receiver class work with spring?

154 Views Asked by At

Because of some context requirement , I have to mark my spring bean context :

context(IConfig)
@Named
class MyService (...) {

   val config = SomeBuilder().build() // it needs IConfig context
}

And the test class :

internal class MyServiceTest : AbstractCoreTest()  {

  @Inject
  private lateinit var myService: MyService
  
  // do tests
}

It compiles OK , but when executing , it throws :

java.lang.IllegalStateException: Failed to load ApplicationContext
 for [MergedContextConfiguration@484876a1 testClass = 
foo.MyServiceTest, locations = ["classpath:core.xml"], 
classes = [], contextInitializerClasses = [destiny.TestAppInit], 
activeProfiles = [], propertySourceLocations = [], propertySourceProperties = [], 
contextCustomizers = [], contextLoader = 
org.springframework.test.context.support.DelegatingSmartContextLoader, parent = null]
    at 
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext
...
...
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'myService' defined in file [.../MyService.class]: Unsatisfied
 dependency expressed through constructor parameter 0: No qualifying bean of type 
'IConfig' available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {}

Is there a better way to solve this ? Thanks.

Kotlin 1.8.21 , Spring 6.0.9

0

There are 0 best solutions below