spring mvc 声明所有 bean 单例 [英] spring mvc declaring all beans singleton

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

问题描述

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

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

我想知道通过将所有 bean 设为默认作用域,我们是否试图实现整个应用程序在单线程中运行?

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?

如果是这样会使每个 httprequest(来自多个或相同会话)排队直到前一个完成?如何避免这种情况,任何建议或链接都​​会有所帮助.

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.

我对 spring 和 java 开发比较陌生.

I am relatively new to spring and java development.

推荐答案

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

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.

事实上,在普通的 servlet 中情况是一样的——每个 servlet 只有一个实例,并且它可以被无限数量的线程访问.只要这个 servlet 是无状态的或正确同步的.

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.

不要在

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