62

I have a query regarding Spring 3 MVC @Controllers. When a request comes in, does the container create a new instance of the controller for each request (Similar to an Action in Struts 2 - ThreadLocal ActionContext) or a single instance responds to all requests? By default are the controller beans singletons in a context?

What are the guidelines/ best practices for a Spring 3 MVC application? Are these settings configurable? Should those be configured? Does it change much if my environment is a clustered environment - I guess it should not as the jvm/containers would be different- but some authoritative suggestion would be welcome.

I have read Spring documentation but probably I missed it. Any 'this is how we implemented kind of' answers/ pointers/ links would be helpful Thanks.

Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40
Atul
  • 2,673
  • 2
  • 28
  • 34

4 Answers4

70

Spring controllers are singletons (there is just one instance of each controller per web application) just like servlets. Typically there is no point in changing this behaviour (if it's even possible). See Regarding thread safety of servlet for common pitfalls, also applying to controllers.

If your application is clustered do as much as you can to avoid state. State in controllers will require synchronization to avoid threading issues. Also you'll probably replicate that state across servers - very expensive and troublesome.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • So are service calls, init binder validations in controller (if the object references are not final) thread safe? Thanks for the link, by the way, it more or less simplified my question. – Atul Jul 16 '12 at 16:39
  • @Atul: well, it totally depends on what you call. If your services, validators, etc. are thread safe (safe to call from multiple threads), then yes. But it's up to you. – Tomasz Nurkiewicz Jul 16 '12 at 16:40
  • 9
    Be aware that they'll only be singletons per context, and that it's entirely possible to have nested contexts when working with Spring web applications, and that by duplicating a package-scan you might end up with multiple instances of the same controller. – DeejUK Jul 16 '12 at 16:55
  • @Deejay, I did not know about duplicate package scans creating multiple component/controller instances. Thanks for this additional info. – Atul Jul 18 '12 at 08:09
  • @EngineerBetter_DJ : I have many `@RestController` classes in my module then will each class will have a different `@RestController`. I would like it that way as just one controller is also capable of handling different resources. Does it make sense ? – Raghuveer Apr 04 '18 at 10:42
  • This behavior can be overridden using [`@Scope("prototype")`](https://stackoverflow.com/a/17074002/). – Alex78191 Apr 29 '18 at 18:24
5

By default, Spring beans are singletons. Spring suggests to use singletons for stateless beans like controllers and DAOs, and prototype scope for stateful beans.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rams
  • 729
  • 10
  • 11
2

The Struts2 Actions are not singletons because they carry state. Struts2 leverages javabeans properties on the action itself to carry the incoming request data and expose it to the various layers of the framework.

Spring, on the other hand, uses a model object that is handed to the controller. The controller itself doesn't hold state, so a singleton makes sense.

chad
  • 7,369
  • 6
  • 37
  • 56
2

Controller are singletons thus can avoid creating a lot of instances by keyword new if the webapp process a lot of requests at the same time.Using controller singleton could also relief the burden of JVM by decreasing young GC.

MrBoy
  • 29
  • 2