Skip to content
Advertisement

micronaut @RequestScope – not creating bean per incoming http-request

I have a class the following class as RequestScope bean:

JavaScript

The idea is to have an object containing the REST request level custom data accessible across the application, the scope of the this obviously should be within the current request. This can be used for say.. logging – whenever devs log anything from the application, some of the request meta data goes with it.

I am not clear what the @RequestScope bean really is:

From its definition – my assumption is it is created for every new http-request and same instance is shared for the life of that request.

when is it constructed by Micronaut ? Is it immutable ?

Across multiple requests I can see the same requestId ( expecting new UUID for every request)

Is it the right use-case for @RequestScope bean?

Advertisement

Answer

when is it constructed by Micronaut ?

A @RequestScope bean is created during request processing, the first time the bean is needed.

Is it immutable ?

It could be. You get to decide if the bean is mutable or not when you write the class. As written in your example, RequestContext is mutable. If you remove the updateRouteName method, that bean would be immutable.

Is it the right use-case for @RequestScope bean?

I don’t think so, but that is really an opinion based question.

EDIT: Based On Comments Added Below

See the project at https://github.com/jeffbrown/rscope.

https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoController.java

JavaScript

https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoBean.java

JavaScript

https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/test/java/rscope/DemoControllerTest.java

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement