Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我习惯于为服务、DAO 或控制器对象创建作为单例的 Spring bean。嗯,这对我来说似乎很自然。现在我的一位同事喜欢将所有此类对象制作为原型。
反对的理由是什么?还是专业的?
我唯一能说明的是每次引用 bean 时创建新实例的开销。虽然我觉得这种做法是非常错误的,肯定还有更多的原因。
所有 bean 都必须在单例范围内,除非有一些非常具体的原因不让它们这样做。一个这样的原因是拥有状态。(默认作用域是单例也就不足为奇了)