1.5. Bean Scopes(Bean作用域)

  Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.5. Bean Scopes(Bean作用域)

1.5.1. The Singleton Scope (单例作用域)

1.5.2. The Prototype Scope(Prototype作用域)

1.5.3. Singleton Beans with Prototype-bean Dependencies(单例和原型Bean的依赖)

1.5.4. Request, Session, Application, and WebSocket Scopes

1.5.4.1. Initial Web Configuration (初始化Web配置)

1.5.4.2. Request scope(请求作用域)

1.5.4.3. Session Scope(会话作用域)

1.5.4.4. Application Scope(应用作用域)

1.5.4.5. Scoped Beans as Dependencies (具有作用域的bean作为依赖项)

1.5.4.5.1 Choosing the Type of Proxy to Create(选择待创建代理的类型)

1.5.5. Custom Scopes (自定义作用域)

1.5.5.1. Creating a Custom Scope(创建自定义作用域)

1.5.5.2. Using a Custom Scope (使用自定义作用域)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.5. Bean Scopes(Bean作用域

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.

当您创建一个bean定义时,您相当于创建了一个菜谱,用于创建由该bean定义(bean definition)定义的类的实际实例。bean定义是一个菜谱的想法很重要,因为这意味着,与类一样,可以从一个菜谱创建多个对象实例。

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 control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because 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. The Spring Framework supports six scopes, four of which are available only if you use a web-aware ApplicationContext. You can also create a custom scope.

The following table describes the supported scopes:

您不仅可以控制要插入到通过特定bean定义创建的对象中的各种依赖项和配置值,还可以控制通过特定bean定义创建的对象的作用域(scope)。这种方法功能强大且灵活,因为您可以选择通过配置创建的对象的作用域(scope),而不必在Java类级别上设置对象的作用域。可以将bean定义为按多个作用域之中的一个作用域部署。Spring框架支持六个作用域,其中四个只有在使用web感知的ApplicationContext时才可用。您还可以创建自定义作用域( a custom scope.)。

下表描述了支持的作用域:

Table 3. Bean scopes

Scope

Description

singleton

(Default) Scopes a single bean definition to a single object instance for each 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.

application

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

websocket

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

Table 3. Bean作用域

作用域

描述

singleton

(默认)为每个Spring IoC容器将单个bean定义的作用域限定为单个对象实例。

prototype

将单个bean定义的作用域限定为任意数量的对象实例。

request

将单个bean定义限定为单个HTTP请求的生命周期。也就是说,每个HTTP请求都有自己的bean实例,该实例是在单个bean定义的后面创建的。仅在支持webSpring ApplicationContext的上下文中有效。

session

将单个bean定义的作用域限定为HTTP会话(HTTP Session)的生命周期。仅在支持webSpring ApplicationContext的上下文中有效。

application

将单个bean定义的作用域限定为ServletContext的生命周期。仅在支持webSpring ApplicationContext的上下文中有效。

websocket

将单个bean定义的作用域限定为WebSocket的生命周期。仅在支持webSpring ApplicationContext的上下文中有效。

As of Spring 3.0, a thread scope is available but is not registered by default. For more information, see the documentation for SimpleThreadScope. For instructions on how to register this or any other custom scope, see Using a Custom Scope.

Spring 3.0开始,可使用线程作用域(thread scope),但默认情况下未注册。有关更多信息,请参阅SimpleThreadScope的文档。有关如何注册线程作用域或任何其他自定义作用域的说明,请参阅使用自定义作用域(Using a Custom Scope)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值