Issue
I have a short question. I know i should annotate a service with @RequestScope or @SessionScope if i want to prevent race conditions and other thread related problems from occuring while handeling multiple incoming requests. But should a Controller have such an annotation as well or should a controller always remain a singleton?
Thank you
Solution
Not really. If your intention is just want to prevent race conditions due to concurrent requests ,you can still achieve it using singleton. Just make sure it does not contain any instance variables which contain some state and controller method will modify these state. In practise , it is very common to inject a stateless singleton service into a singleton controller. Theoretically , the singleton controller should have the best performance as it does not need to be created every times.
What scope to use actually depends on the use-cases. For example, if you are implementing a web-based shopping cart , you definitely need to look at the session scope controller such that items added to the cart will not disappear between two HTTP requests in the same session.
My experience of implementing the RESTful web service is that I only use singleton controller as one of the characterise of the RESTful web service is stateless.
Answered By - Ken Chan