单例和原型bean有什么区别? [英] What is difference between singleton and prototype bean?

查看:320
本文介绍了单例和原型bean有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


基本上,一个bean的范围定义了它们在应用程序上的存在。 p>

Singleton:指每个Spring IOC容器对单个对象实例的单个bean定义。



原型:指单个bean定义到任何数量的对象实例。


那么什么是对象实例。

解决方案

原型范围 =每次注入/查找时都会创建一个新对象。每次都会使用 new SomeClass()



Singleton范围 = (默认)每次注入/查找时都会返回相同的对象。在这里,它将实例化一个 SomeClass 的一个实例,然后每次返回。



另请参见: / strong>




i'm new to spring and i read this :

Basically a bean has scopes which defines their existence on the application

Singleton: means single bean definition to a single object instance per Spring IOC container.

Prototype: means a single bean definition to any number of object instances.

So What is the "object instance" .

解决方案

Prototype scope = A new object is created each time it is injected/looked up. It will use new SomeClass() each time.

Singleton scope = (Default) The same object is returned each time it is injected/looked up. Here it will instantiate one instance of SomeClass and then return it each time.

See also:

这篇关于单例和原型bean有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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