11

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.

Sambit
  • 7,625
  • 7
  • 34
  • 65

1 Answers1

14

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.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724