I’m trying to create Micronaut bean with @RequestScope, but with each injection, there is a new instance created. I would expect it to create new instance only once per request.
Here is an example code:
@RequestScope
class RequestContext {
var foo: String? = null
}
@Controller("/example")
class ExampleController(
val requestContext: RequestContext,
val exampleService: ExampleService
) {
@Get()
fun example() {
requestContext.foo = "foo"
exampleService.example()
}
}
@Singleton
class ExampleService(
val requestContextProvider: RequestContext
) {
fun example() {
println(requestContextProvider.foo) // prints nul instead of "foo"
}
}
I have also tried wrapping RequestContext in Provider like here: micronaut @RequestScope – not creating bean per incoming http-request
but it changes nothing.
What am doing wrong?
How can I properly inject RequesScope bean into Singleton?
Micronaut Version: 4.5.1
Kotlin Version: 1.9.23
Tomasz Świstak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.