IT忍者神龟之Bean scope

3.5 Bean scopes

When you create a bean definition, you create a recipe(配方) 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 the scope of the objects created from a particular bean definition. This approach(做法) is powerful(强大) and flexible(灵活) in that you can choose the 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-aware ApplicationContext.

The following scopes are supported out of the box. You can also create a custom scope.

Table 3.3. Bean scopes

Scope Description

singleton

(Default) Scopes a single bean definition to a single object instance per(每个) Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

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 ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.


[Note] Thread-scoped beans

As of Spring 3.0, a thread scope is 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, see Section 3.5.5.2, “Using a custom scope”.


3.5.1 The singleton scope

Only one shared instance 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 creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent(后来的) requests and references for 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 one and only one(有且仅有一个) instance of a particular(指定,特定) class is created per ClassLoader. The scope of the Spring singleton is best described as per 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 one and only one instance 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.2 The prototype scope

The non-singleton, prototype scope of bean deployment(部署) results in the creation of a new bean instance every time a request for that specific bean is made. That is(就是说), the bean is injected into another bean or you request it through a getBean() 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.4 Request, session, and global session scopes

The requestsession, and global session scopes are only available if you use a web-aware Spring ApplicationContext implementation (such asXmlWebApplicationContext). If you use these scopes with regular Spring IoC containers such as the ClassPathXmlApplicationContext, you get an IllegalStateException complaining about an unknown bean scope.

你可能感兴趣的:(IT忍者神龟之Bean scope)