Horse Voice
Horse Voice

Reputation: 8338

Sharing data across Spring MVC Controller methods

In other MVC frameworks, the data across controller methods is not sharable because each request that comes in to the controller is an independent request. How does spring do this? Does it initialize one instance of the controller and that same instance is used to handle all requests? If its the same controller singleton then lets say I have a service @Autowired, what happens there? Would there just be that one instance of service that will be used?

Thanks in advance.

Upvotes: 1

Views: 1653

Answers (2)

Tom Johns
Tom Johns

Reputation: 985

There is a nice article which talks about singleton and thread safety at https://tarunsapra.wordpress.com/2011/08/21/spring-singleton-request-session-beans-and-thread-safety/

Upvotes: 0

Vimal Mathew
Vimal Mathew

Reputation: 575

Yes, Spring controllers are singletons, there is just one instance of each controller per web application. The @Repository, @Service and @Controller are all by default singletons as they don’t have their own state but every thread accessing them perform certain thread specific operation thus these beans remains unaffected by it’s own concurrent access because it doesn’t have it’s own state.

Upvotes: 1

Related Questions