---
title: "Spring bean scope"
author: "xingda1989"
date: "2011-01-21T21:48:26Z"
tags: ["Bean", "Spring", "Web", "prototype"]
categories: ["企业架构"]
slug: "spring-bean-scope"
draft: false
lastmod: "2011-01-21T21:48:26Z"
id: 891207
url: "https://xingda1989.iteye.com/blog/891207"
---

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