Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
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.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.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.5.2. Using a Custom Scope (使用自定义作用域)
After you write and test one or more custom Scope
implementations, you need to make the Spring container aware of your new scopes. The following method is the central method to register a new Scope
with the Spring container:
在编写和测试一个或多个自定义作用域实现之后,您需要让Spring容器知道您的新作用域。以下方法是向Spring容器注册新作用域(Scope
)的中心方法(central method):
Java
void registerScope(String scopeName, Scope scope);
Kotlin
fun registerScope(scopeName: String, scope: Scope)
This method is declared on the ConfigurableBeanFactory
interface, which is available through the BeanFactory
property on most of the concrete ApplicationContext
implementations that ship with Spring.
此方法在ConfigurableBeanFactory
接口上声明,该接口可通过Spring附带的大多数具体ApplicationContext
实现的BeanFactory属性获得。
The first argument to the registerScope(..)
method is the unique name associated with a scope. Examples of such names in the Spring container itself are singleton
and prototype
. The second argument to the registerScope(..)
method is an actual instance of the custom Scope
implementation that you wish to register and use.
Suppose that you write your custom Scope
implementation, and then register it as shown in the next example.
registerScope(..)
方法的第一个参数是与作用域关联的唯一名称(unique name associated with a scope)。Spring容器中此类名称的示例有singleton和prototype。 registerScope(..)
方法的第二个参数是您希望注册和使用的自定义作用域实现(custom Scope
implementation)的实际实例。
假设编写自定义作用域实现(custom Scope
implementation),然后注册它,如下一个示例所示。
The next example uses |
下一个示例使用 |
Java
Scope threadScope = new SimpleThreadScope() ;
beanFactory.registerScope("thread", threadScope);
Kotlin
Val threadScope = SimpleThreadScope()
beanFactory.registerScope("thread", threadScope)
You can then create bean definitions that adhere to the scoping rules of your custom Scope
, as follows:
然后,您可以创建符合您的自定义Scope
的作用域规则(scoping rule)的bean定义,如下所示:
<bean id="..." class="..." scope="thread">
With a custom Scope
implementation, you are not limited to programmatic registration of the scope. You can also do the Scope
registration declaratively, by using the CustomScopeConfigurer
class, as the following example shows:
使用自定义Scope
实现,您不限于通过编程注册作用域。您还可以使用CustomScopeConfigurer
类以声明方式进行作用域注册,如下例所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread">
<bean class="org.springframework.context.support.SimpleThreadScope"/>
</entry>
</map>
</property>
</bean>
<bean id="thing2" class="x.y.Thing2" scope="thread">
<property name="name" value="Rick"/>
<aop:scoped-proxy/>
</bean>
<bean id="thing1" class="x.y.Thing1">
<property name="thing2" ref="thing2"/>
</bean>
</beans>
When you place |
当您将 |