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
To support the scoping of beans at the request
, session
, application
, and websocket
levels (web-scoped beans), some minor initial configuration is required before you define your beans. (This initial setup is not required for the standard scopes: singleton
and prototype
.)
How you accomplish this initial setup depends on your particular Servlet environment.
为了在请求、会话、应用(request
, session
, application
)和websocket级别(web范围的bean)支持bean的作用域,在定义bean之前需要进行一些较小的初始配置(标准作用域(singleton和prototype)不需要此初始设置。)
如何完成初始设置取决于特定的Servlet环境。
If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the Spring DispatcherServlet
, no special setup is necessary. DispatcherServlet
already exposes all relevant state.
如果您在Spring Web MVC中访问具有作用域bean(实际上是在Spring DispatcherServlet
处理的请求中),则不需要特殊设置。DispatcherServlet
已公开所有相关状态。
If you use a Servlet 2.5 web container, with requests processed outside of Spring’s DispatcherServlet
(for example, when using JSF or Struts), you need to register the org.springframework.web.context.request.RequestContextListener
ServletRequestListener
. For Servlet 3.0+, this can be done programmatically by using the WebApplicationInitializer
interface. Alternatively, or for older containers, add the following declaration to your web application’s web.xml
file:
如果使用Servlet 2.5 web容器,请求在Spring的DispatcherServlet
之外处理(例如,当使用JSF或Struts时),则需要注册org.springframework.web.context.request.RequestContextListener
ServletRequestListener
。对于Servlet 3.0+,这可以通过使用WebApplicationInitializer
接口以编程方式完成。或者,对于旧容器,将以下声明添加到web应用的web.xml文件中:
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
Alternatively, if there are issues with your listener setup, consider using Spring’s RequestContextFilter
. The filter mapping depends on the surrounding web application configuration, so you have to change it as appropriate. The following listing shows the filter part of a web application:
或者,如果您的侦听器设置(listener setup)存在问题,请考虑使用Spring的RequestContextFilter
。过滤器映射(filter mapping)取决于web应用程序的配置,因此必须根据需要进行更改。以下列表展示了web应用程序的过滤器部分(filter part):
<web-app>
...
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
DispatcherServlet
, RequestContextListener
, and RequestContextFilter
all do exactly the same thing, namely bind the HTTP request object to the Thread
that is servicing that request. This makes beans that are request- and session-scoped available further down the call chain.
DispatcherServlet
、RequestContextListener
和RequestContextFilter
都执行完全相同的操作,即将HTTP请求对象绑定到为该请求提供服务的线程。这使得具有请求和会话作用域(request- and session-scoped)的bean在调用链的更下游可用。