设计:Spring 中的无状态原型
我习惯于为 Service、DAO 或 Controller 对象创建 Spring bean 作为单例。嗯,这对我来说似乎很自然。现在我的一位同事喜欢制作所有此类对象的原型。
反对的理由是什么?还是专业人士?
我唯一能说的是每次引用 bean 时创建新实例的开销。虽然我觉得这种做法是非常错误的,而且肯定还有更多的原因。
I got used to create Spring beans for Service, DAO or Controller objects as Singletons. Well, it seems natural to me. Now a colleague of mine likes to make all such objects Prototypes.
What can be the reasons against that? Or pro?
The only thing I can state is the overhead of new instance creation every time the bean is referenced. Though I feel this approach is very wrong and there must be more reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有 bean 都必须在单例范围内,除非有一些非常具体的原因不这样做。其中一个原因就是拥有状态。 (默认范围是单例也就不足为奇了)
All beans must be in singleton scope, unless there's some very specific reason for them not to be. One such reason is having state. (It is no surprise that the default scope is singleton)