开发者

When to use request scoped beans over singleton beans for Controllers in Spring MVC

Is it to ensure that the controllers are thread safe?

If the above case is true, then is it necessary to go through the over开发者_高级运维head of bean creation for each request rather than making the controller code not rely on instance variables?


Your default position should be to use singleton controllers which are thread-safe. This partly for performance reasons, as you say, and partly for reasons of good design - a large mass of stateful, request-scoped beans is a mess.

Using request-scoped controllers (or other request-scoped beans) is a specialised requirement which you should only use when you have good reason to do so, i.e. you have beans whose state must be private to the lifecycle of that particular request.


Request scoped beans are short living instances of a class, they will be created when a new request comes in.

Singleton beans live the entire lifetime of your application. Note: If you have a multi user application with several sessions, all users will access the same instance of your beans, if they are singletons.

I would prefer Request scoped beans whenever possible in a web application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜