In case of REST implementations in Spring, spring Controllers are singleton. I want to know why spring controllers are singleton apart from thread-safety issue. Please help in resolving this issue.
This has nothing to do with REST.
Spring beans are, by default, singleton scoped. Since component scanning a @Controller
annotated class simply generates a bean, that bean will be singleton scoped.
For reasons why a @Controller
bean should be stateless, read any and all of the following:
To follow up on the REST question, REST is meant to be stateless. In other words, each request contains all the information it needs for the server to handle it. Knowing that, it's pointless for the server (or @Controller)
to keep any information after it's finished handling the request in instance fields and the like. Therefore a singleton is the way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With