java spring有应用范围吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44361580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there application Scope in spring?
提问by Shakthi
I saw 'application' scope in the following blog. Is it true?
我在以下博客中看到了“应用程序”范围。这是真的吗?
Because, as per my surfing, I got to know spring has only the below 5 scopes. Please correct me if I'm wrong.
因为,根据我的冲浪,我知道 spring 只有以下 5 个范围。如果我错了,请纠正我。
- Singleton
- Prototype
- Request
- Session
- Global Session
- 单身人士
- 原型
- 要求
- 会议
- 全球会议
回答by Pau
There is a section on the official doc which is related to the bean scopes:
官方文档中有一节与 bean 作用域相关:
Basically, they define the next:
基本上,他们定义了下一个:
singleton(Default) Scopes a single bean definition to a single object instance per Spring IoC container.
prototypeScopes a single bean definition to any number of object instances.
requestScopes 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.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
globalSessionScopes 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.
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
单例(默认)将单个 bean 定义范围限定为每个 Spring IoC 容器的单个对象实例。
原型将单个 bean 定义范围限定为任意数量的对象实例。
request将单个 bean 定义范围限定为单个 HTTP 请求的生命周期;也就是说,每个 HTTP 请求都有自己的 bean 实例,该 bean 实例是在单个 bean 定义的后面创建的。仅在 web-aware Spring ApplicationContext 的上下文中有效。
session将单个 bean 定义范围限定为 HTTP 会话的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。
globalSession将单个 bean 定义范围限定为全局 HTTP 会话的生命周期。通常仅在 Portlet 上下文中使用时才有效。仅在 web-aware Spring ApplicationContext 的上下文中有效。
application将单个 bean 定义范围限定为 ServletContext 的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。
websocket将单个 bean 定义范围限定为 WebSocket 的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。
Furthermore, as Spring 3.0 exists other scope thread scopebut is not registered by default, moreover you could even create your own scope:
此外,由于 Spring 3.0 存在其他作用域 线程作用域但默认情况下未注册,因此您甚至可以创建自己的作用域:
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 another custom scope, see the section called “Using a custom scope”.
从 Spring 3.0 开始,线程范围可用,但默认情况下未注册。有关更多信息,请参阅 SimpleThreadScope 的文档。有关如何注册此或任何其他自定义范围的说明,请参阅名为“使用自定义范围”的部分。
There is a section which explains how to define your custom scope:
有一个部分解释了如何定义自定义范围:
Respect to Application scope
, they define it as next:
尊重Application scope
,他们将其定义为下一个:
The Spring container creates a new instance of the AppPreferences bean by using the appPreferences bean definition once for the entire web application. That is, the appPreferences bean is scoped at the ServletContext level, stored as a regular ServletContext attribute.
Spring 容器通过为整个 Web 应用程序使用 appPreferences bean 定义一次来创建 AppPreferences bean 的新实例。也就是说,appPreferences bean 的范围在 ServletContext 级别,存储为常规 ServletContext 属性。
It also explains the difference between a Spring singleton bean:
它还解释了 Spring 单例 bean 之间的区别:
This is somewhat similar to a Spring singleton bean but differs in two important ways: It is a singleton per ServletContext, not per Spring 'ApplicationContext' (for which there may be several in any given web application), and it is actually exposed and therefore visible as a ServletContext attribute
这有点类似于 Spring 单例 bean,但在两个重要方面有所不同:它是每个 ServletContext 的单例,而不是每个 Spring 'ApplicationContext'(在任何给定的 Web 应用程序中可能有多个),并且它实际上是公开的,因此作为 ServletContext 属性可见
So in case you are looking to use with XML:
因此,如果您希望与 XML 一起使用:
<bean id="apps" class="com.App" scope="application"/>
Or annotation:
或注解:
@ApplicationScope
@Component
public class App {
// ...
}
回答by Lakshman Miani
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
application将单个 bean 定义范围限定为 ServletContext 的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。
Follow the link for more details: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes
点击链接了解更多详情:http: //docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes