Search code examples
kotlinmicronaut

How to get ApplicationContext in Micronaut CLI application into Httpclient class?


I am running a Micronaut CLI application via Picocli runner wanted to use applicationContext for injecting/getting the bean during application execution.

@HttpRetryable
@Client("SERVICE_ID")
interface TestHttpClient {

}
@Inject
lateinit var applicationContext: ApplicationContext

private class AuthenticationFilter() : HttpClientFilter {
    // calling appcontext here
    println("appContext is Running " + appContext.isRunning)
}

But getting this error on the execution

kotlin.UninitializedPropertyAccessException: lateinit property applicationContext has not been initialized```
How this can be fixed ?

Solution

  • The property needs to be in the AuthenticationFilter class. That class should be annotated with jakarta.inject.Singleton and should not be private