弹簧接线,单例与原型 [英] spring wiring, singleton versus prototype

查看:68
本文介绍了弹簧接线,单例与原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在春季阅读,并在春季使用DI时,如果将bean设置为单例(默认),则该类的单个实例将被分配,而原型每次都强制一个新实例

just reading up on spring, and when using DI in spring, if you set the bean to be a singleton (default), then a single instance of the class will be 'dispensed', while prototype forces a new instance each time.

每次容器分配相同实例的后果是什么?
是否意味着会有共享状态?

What are the ramifications of having the same instance dispensed by the container each time? does that mean there will be shared state?

即如果在某个地方修改了实例,将来所有创建的对象实际上将得到一个脏对象,而不是一个新对象?

i.e. if the instance is modified somewhere, all future object creations will actually be getting a 'dirty' object and not a 'new' one?

推荐答案

由于通常是使用Spring编程接口,因此DI管理的对象无需担心状态。在这种情况下,无需担心肮脏。存储库,服务,控制器-都应该很少或根本没有状态可言。

Since you're usually programming to an interface with Spring, the objects that are DI managed don't have state to worry about. There's no concern about "dirty" in that case. Repositories, services, controllers - all should have little or no state to speak of.

这篇关于弹簧接线,单例与原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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