3.5Bean scopes
When you create a bean definition, you create arecipe(配方)for creating actual(真实的,实际的) instances of the class defined by that bean definition. The idea(想法) that a bean definition is a recipe is important(重要的), because it means that, as with a class, you can create many object instances from a single recipe.
You can control not only the various(不同的) dependencies and configuration values that are to be plugged into(插入到) an object that is created from a particular bean definition, but also thescopeof the objects created from a particular bean definition. This approach(做法) is powerful(强大) and flexible(灵活) in that you canchoosethe scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. Beans can be defined to be deployed(部署) in one of a number of scopes: out of the box, the Spring Framework supports five scopes, three of which are available only if you use a web-awareApplicationContext
.
The following scopes are supported out of the box. You can also createa custom scope.
Table3.3.Bean scopes
Scope | Description |
---|---|
(Default) Scopes a single bean definition to a single object instance per(每个) Spring IoC container. | |
Scopes a single bean definition to any number of object instances. | |
Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring | |
Scopes a single bean definition to the lifecycle of an HTTP | |
Scopes a single bean definition to the lifecycle of a global HTTP |
Thread-scoped beans | |
---|---|
As of Spring 3.0, athread scopeis available, but is not registered by default. For more information, see the documentation forSimpleThreadScope. For instructions on how to register this or any other custom scope, seeSection3.5.5.2, “Using a custom scope”. |
3.5.1The singleton scope
Only onesharedinstance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result in that one specific bean instance being returned by the Spring container.
To put it another way(换句话说), when you define a bean definition and it is scoped as a singleton, the Spring IoC container createsexactly oneinstance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, andall subsequent(后来的) requests and referencesfor that named bean return the cached object.
Spring's concept(概念) of a singleton bean differs from the Singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the scope of an object such that oneand only one(有且仅有一个)instance of a particular(指定,特定) class is createdperClassLoader
. The scope of the Spring singleton is best described asper container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container creates oneand only oneinstance of the class defined by that bean definition.The singleton scope is the default scope in Spring. To define a bean as a singleton in XML, you would write, for example:
3.5.2The prototype scope
The non-singleton, prototype scope of bean deployment(部署) results in thecreation of a new bean instanceevery time a request for that specific bean is made. That is(就是说), the bean is injected into another bean or you request it through agetBean()
method call on the container. As a rule(一般来说), use the prototype scope for all stateful(有状态)beans and the singleton scope for stateless(无状态) beans.
The following diagram illustrates(图表阐述) the Spring prototype scope.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.
3.5.4Request, session, and global session scopes
The
request
,session
, andglobal session
scopes areonlyavailable if you use a web-aware SpringApplicationContext
implementation (such asXmlWebApplicationContext
). If you use these scopes with regular Spring IoC containers such as theClassPathXmlApplicationContext
, you get anIllegalStateException
complaining about an unknown bean scope.