何时使用Spring原型范围? [英] When to use Spring prototype scope?

查看:131
本文介绍了何时使用Spring原型范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我应该在Spring什么时候使用原型范围?我已经知道,如果请求bean, singleton 将返回相同的对象实例。

I want to know when should i exactly use the prototype scope in Spring? I have understood that singleton returns the same object instance if the bean is requested for.

那我们为什么要考虑 prototype

使用示例的解释将有助于理解它的必要性。

Explanations with examples would help a lot to understand the need for it.

推荐答案

要清楚简单的定义:


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

Singleton scope =每个返回相同的对象注入/抬头的时间。这里它将实例化一个 SomeBean 的实例,然后每次都返回它。

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

原型bean在使用时创建。因此,当您希望拥有有状态bean时,有时需要具有原型范围,或者您不习惯在bean中缓存任何值。原型bean可以与一个会话或一些调用相关联。

Prototype bean is created at the time of usage. So when you would like to have statefull beans there is strong need sometimes to have prototypes scope or when you don't wont to cache any values in beans. Prototype bean can be associated with one session or some call.

示例:

数据访问对象(DAO) )通常不配置为原型,因为典型的DAO不具有任何会话状态;这个作者只是更容易重用单例图的核心。

A data access object (DAO) is not typically configured as a prototype, because a typical DAO does not hold any conversational state; it was just easier for this author to reuse the core of the singleton diagram.

这篇关于何时使用Spring原型范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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