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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:10:53  来源:igfitidea点击:

Differences : @SessionScoped vs @Stateful and @ApplicationScoped vs @Singleton

javasingletonejbstatelessstateful

提问by Gugelhupf

I would like to know, what are the principal differencesbetween :

我想知道,以下之间的主要区别是什么:

  1. javax.enterprise.context.SessionScopedand javax.ejb.Stateful
  2. javax.enterprise.context.ApplicationScoped and javax.ejb.Singleton
  1. javax.enterprise.context.SessionScopedjavax.ejb.Stateful
  2. 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

@SessionScopeddenotes a scope while @Statefulis in a way what we would now call a stereotype. @Statefuladds a number a services to a bean, among which transactional behavior and passivation.

@SessionScoped表示一个范围,而@Stateful在某种程度上我们现在称之为构造型。@Stateful向 bean 添加多项服务,其中包括事务行为和钝化。

Central to @Statefulis 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 @Statefulis an open-ended user managed session, with its lifetime managed by a client that has a reference to the bean proxy.

不同之处在于会话范围与 HTTP 会话相关联,@Stateful而是一个开放式用户管理的会话,其生命周期由具有对 bean 代理的引用的客户端管理。

@Statefulremote beans where originally the binary (RMI) counter parts of Servlets. Where Servlets listened to remote HTTP requests from a browser, @Statefulremote 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 @Statefulbeans 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 @StatefulEJB every separate bean has its own session and component namespace.

不幸的是,两者之间存在许多不一致之处。Servlet 只是一个 HTTP 侦听器,而@Statefulbean 会自动引入许多其他功能。一个 Servlet 还与所有其他 Servlet 共享会话,并在War中与所有其他 Servlet 共享 Java EE 组件命名空间,而对于@StatefulEJB,每个单独的 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 @Statefulbean has become less clear.

随着 EJB 2 中本地 bean 的引入以及用于远程 EJB 通信的 Swing/Applet 客户端的急剧下降,为@Statefulbean维护的会话的功能变得不那么清晰了。

I think it's fair to say that @Statefulsimply 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 @Statelessbeans and/or CDI beans for business logic.

我认为可以公平地说,@Stateful这些天根本没有那么多使用。对于 Web 应用程序,HTTP 会话几乎总是领先的,这意味着使用会话范围和本地@Statelessbean 和/或 CDI bean 进行业务逻辑。

In some cases @Statefulbeans 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 @Statefuland @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 @ApplicationScopedand @Singleton, although without the legacy (@Singletonis a fairly new thing). @ApplicationScopedis just a scope, while @Singletonis 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 @Statefuland @Singletonare 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定型。