Java 区别:@SessionScoped vs @Stateful 和@ApplicationScoped vs @Singleton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24105807/
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
Differences : @SessionScoped vs @Stateful and @ApplicationScoped vs @Singleton
提问by Gugelhupf
I would like to know, what are the principal differencesbetween :
我想知道,以下之间的主要区别是什么:
- javax.enterprise.context.SessionScopedand javax.ejb.Stateful
- javax.enterprise.context.ApplicationScoped and javax.ejb.Singleton
- javax.enterprise.context.SessionScoped和javax.ejb.Stateful
- javax.enterprise.context.ApplicationScoped 和 javax.ejb.Singleton
I know that a @SessionScopedand a @Statefulallows to create a new instance for each client. I also know that for the @ApplicationScopedand @Singleton / @Statelessthey are shared between the clients.
我知道@SessionScoped和@Stateful允许为每个客户端创建一个新实例。我也知道对于@ApplicationScoped和@Singleton / @Stateless它们在客户端之间共享。
=> But when should I considerit's better to choose an EJB, or the other?
=> 但是我什么时候应该考虑选择 EJB 或另一个更好?
回答by Arjan Tijms
@SessionScoped
denotes a scope while @Stateful
is in a way what we would now call a stereotype. @Stateful
adds a number a services to a bean, among which transactional behavior and passivation.
@SessionScoped
表示一个范围,而@Stateful
在某种程度上我们现在称之为构造型。@Stateful
向 bean 添加多项服务,其中包括事务行为和钝化。
Central to @Stateful
is however its session behavior, which indeed overlaps with the session scope.
@Stateful
然而,核心是它的会话行为,它确实与会话范围重叠。
The difference is that the session scope is tied to the HTTP session, while @Stateful
is an open-ended user managed session, with its lifetime managed by a client that has a reference to the bean proxy.
不同之处在于会话范围与 HTTP 会话相关联,@Stateful
而是一个开放式用户管理的会话,其生命周期由具有对 bean 代理的引用的客户端管理。
@Stateful
remote beans where originally the binary (RMI) counter parts of Servlets. Where Servlets listened to remote HTTP requests from a browser, @Stateful
remote beans listened to remote RMI requests from Applets (and later Swing clients).
@Stateful
远程 bean,最初是 Servlet 的二进制 (RMI) 计数器部分。Servlet 侦听来自浏览器的远程 HTTP 请求,而@Stateful
远程 bean 侦听来自 Applet(以及后来的 Swing 客户端)的远程 RMI 请求。
There were unfortunately many inconsistencies between the two. A Servlet was just an HTTP listener, while @Stateful
beans automatically brought in a lot of other features. A Servlet also shared the session with all other Servlets and also shared the Java EE component namespace with all other Servlets in a war, while with the @Stateful
EJB every separate bean has its own session and component namespace.
不幸的是,两者之间存在许多不一致之处。Servlet 只是一个 HTTP 侦听器,而@Stateful
bean 会自动引入许多其他功能。一个 Servlet 还与所有其他 Servlet 共享会话,并在War中与所有其他 Servlet 共享 Java EE 组件命名空间,而对于@Stateful
EJB,每个单独的 bean 都有自己的会话和组件命名空间。
With the introduction of local beans in EJB 2 and a sharp decline of Swing/Applet clients for remote EJB communication, the function of the session that's maintained for an @Stateful
bean has become less clear.
随着 EJB 2 中本地 bean 的引入以及用于远程 EJB 通信的 Swing/Applet 客户端的急剧下降,为@Stateful
bean维护的会话的功能变得不那么清晰了。
I think it's fair to say that @Stateful
simply isn't used that much these days. For a web application the HTTP session is almost always leading, which means using the session scope and local @Stateless
beans and/or CDI beans for business logic.
我认为可以公平地说,@Stateful
这些天根本没有那么多使用。对于 Web 应用程序,HTTP 会话几乎总是领先的,这意味着使用会话范围和本地@Stateless
bean 和/或 CDI bean 进行业务逻辑。
In some cases @Stateful
beans are needed for their natural support for the extended persistence context from JPA and for their passivation features (Servlet doesn't have a standardized passivation mechanism). Note that @Stateful
and @SessionScoped
(or many other scopes) can be combined. The advantage of combining them is that user code no longer needs to manage the lifetime, but the container manages this.
在某些情况下@Stateful
,需要 bean 来自然支持来自 JPA 的扩展持久性上下文及其钝化功能(Servlet 没有标准化的钝化机制)。请注意,@Stateful
和@SessionScoped
(或许多其他范围)可以组合使用。将它们结合起来的好处是用户代码不再需要管理生命周期,而是由容器来管理。
There's a somewhat similar story for @ApplicationScoped
and @Singleton
, although without the legacy (@Singleton
is a fairly new thing). @ApplicationScoped
is just a scope, while @Singleton
is a bean type (stereotype if you wish), which doesn't only give you application scoped behavior, but also provides you with transactional behavior again, with automatic locking (which can be tuned via @Lock
) and with eager construction behavior (via @Startup
).
有几分相似的故事@ApplicationScoped
和@Singleton
,虽然没有传统的(@Singleton
是一个相当新的东西)。@ApplicationScoped
只是一个范围,而@Singleton
是一个 bean 类型(如果你愿意的话,是原型),它不仅为你提供应用程序范围的行为,而且还为你提供了事务行为,具有自动锁定(可以通过 调整@Lock
)和eager构造行为(通过@Startup
)。
Although @Stateful
and @Singleton
are by themselves pretty handy, the current way forward in Java EE seems to be to decompose those build-in stereotypes into separately useable annotations and who knows, perhaps one day they will be actual CDI stereotypes consisting of those decomposed annotations.
虽然@Stateful
和@Singleton
它们本身非常方便,在Java EE正向电流的方式似乎是那些内置的定型分解为单独可用的注释,谁知道,也许有一天,他们将是由这些分解注释的实际CDI定型。