春天mvc宣布所有的豆单 [英] spring mvc declaring all beans singleton

查看:95
本文介绍了春天mvc宣布所有的豆单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个新的mvc项目,其中所有的bean都是默认的范围(没有原型或会话)。
与单个应用程序上下文。



我想知道
通过使所有的bean都是默认的范围是我们试图实现整个应用程序运行在单线程?



是否会使每个httprequest(从多个或相同的会话)排队等到上一个完成?如何避免这种情况任何建议或链接将是有帮助的。



我对spring和java开发相对较新。

解决方案

因为Spring bean通常是无状态的,所以可以安全地从多个线程中调用它们。这就是你的应用程序的工作原理:每个控制器,服务,DAO等只有一个实例。但是你的servlet容器(通过Spring)从多个线程调用这些bean,而且它完全线程安全。



事实上,在简单的servlet中,情况是一样的 - 每个servlet只有一个实例,它可以被无限数量的线程访问。只要这个servlet是无状态的或正确同步的。



不要将Spring与无状态会话bean混淆在 ejb ,它们是池,每个客户端从池中获取自己的实例。 1



1 - 实际上这有点愚蠢 - 由于bean是无状态的定义,没有任何意义来合并它们,并阻止并发访问...


I have this new mvc project where all beans are default scoped(no prototype or session). with single application context.

i want to know by making all beans to be default scoped are we trying to achieve the whole application to be run in single thread?

if so will that make each httprequest(from multiple or same sessions) to be queued until the previous one completes?How to avoid such scenario any advice or link would be helpful.

I am relatively new to spring and java development.

解决方案

Because Spring beans are typically stateless, you can safely call them from multiple threads. That's how your application works: there is only one instance of every controller, service, DAO, etc. But your servlet container (through Spring) calls these beans from multiple threads - and it's completely thread safe.

In fact in plain servlets the situation is the same - there is only instance of each servlet and it can be accessed by infinite number of threads. As long as this servlet is stateless or properly synchronized.

Do not confuse Spring with stateless session beans in that are pooled and each client gets its own instance from the pool.1

1 - In fact that's a bit dumb - since the beans are stateless by the definition, there is no point in pooling them and preventing concurrent access...

这篇关于春天mvc宣布所有的豆单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆