ITeye博客

Spring bean scope

explore · 2010-04-28 17:59 · J2EE · 企业架构 · Markdown

Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope类型:request、session和global session,这三种只能在web 应用中才可以使用。

 

singleton: Spring 容器只会创建该bean定义的唯一实例,这个实例会被保存到缓存中,并且对该bean的所有后续请求和引用都将返回该缓存中的对象实例,一般情况下,无状态的bean使用该scope。

 

prototype:每次对该bean的请求都会创建一个新的实例,一般情况下,有状态的bean使用该scope。

 

request:每次http请求将会有各自的bean实例,类似于prototype。

 

session:在一个http session中,一个bean定义对应一个bean实例。

 

global session:在一个全局的http session中,一个bean定义对应一个bean实例。典型情况下,仅在使用portlet context的时候有效。

 

对于后三种,在实践中没有用过,理解不深。

评论(2)

  1. #2 楼 · explore · 2010-08-02 19:16

    singleton

    tomcatlee writes:

    默认是什么scope?

    默认是singleton

  2. #1 楼 · tomcatlee · 2010-08-01 12:20

    默认是什么scope?

评论功能维护中,当前仅展示历史评论。