---
title: "Spring bean scope"
author: "explore"
date: "2010-04-28T17:59:59Z"
tags: []
categories: ["企业架构"]
slug: "spring-bean-scope"
draft: false
lastmod: "2010-04-28T17:59:59Z"
id: 655475
url: "https://explore.iteye.com/blog/655475"
---

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的时候有效。

 

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