ITeye博客

Spring bean scope

xingda1989 · 2011-01-21 21:48 · spring · 企业架构 · Spring · prototype · Web · Bean · 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的时候有效。

评论(0)

暂无评论。

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