JAVA EE CDI 范围、EJB 和托管 bean 序列化 [英] JAVA EE CDI Scopes, EJBs and managed beans serialization

查看:26
本文介绍了JAVA EE CDI 范围、EJB 和托管 bean 序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对作用域、ejb 和托管 bean 有一些疑问.

Have some questions regarding scopes, ejbs, and managed beans.

  1. 范围(javax.enterprise.context.ApplicationScope、javax.enterprise.context.SessionScope)是否仅适用于 EJB?或者它们适用于所有托管 bean?直到今天,我非常确定它适用于所有托管 bean.
  2. 在我的应用程序中,我们有:

  1. Are scopes (javax.enterprise.context.ApplicationScope, javax.enterprise.context.SessionScope) only for EJBs? Or are they for all managed beans? Until today I was pretty sure it was for all managed beans.
  2. In my application we have:

@ApplicationScoped
public class MyClass implements MyNonSerializableInterface {
  @Inject  
  private transient NonSerializableLogger transientLogger;
  @Inject
  private NonSerializableLogger logger;
 ...
}

和一些经理:

@Singleton
public class SomeManager {
    @Inject private MyClass myClass;  

}

和网络服务:

@Path("some")
public class SomeWebService {
    @Inject private SomeManager;

}

容器(部署时间)或编译器不报错,正常吗?

The container (deploy time) or compiler does not complain about it, is that normal?

我认为:

使用会话、应用程序或对话范围的 Bean 必须可序列化,但使用请求范围的 Bean 不必可序列化."JAVA EE 使用范围

"Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable."JAVA EE Using Scopes

MyClass 是否应该实现 Serializable?我们可以说因为托管 bean 被注入到 @Singleton 中,所以不会发生序列化吗?因此在部署时没有显示序列化错误?

MyClass should implement Serializable or not? Could we say that because the managed bean is injected into a @Singleton, serialization never occurs? Therefore no serialization error is shown at deploy time?

  1. 如果是:如果我创建 MyClass @ApplicationScoped 和 @Stateful 并使用 @EJB 将其注入 SomeManager,那么在部署时我确实会收到有关序列化的错误..
  2. 如果否:为什么我没有为瞬态记录器获得一些 NullPointerExceptions(由于钝化/激活)?

推荐答案

CDI 范围在 CDI 容器的上下文中进行评估.也就是说,CDI 规范的设计者确保它可以与 EJB 和 jsf Managed Bean 一起使用.那就是说.

CDI scopes are evaluated in the context of CDI container. That said, the designers of CDI specification ensured that it is operable with EJB and jsf Managed Bean. That said.

  1. CDI 范围在理想情况下是对使用上下文敏感的.@ApplicationScoped 意味着 CDI bean 应该从它创建的实例到应用程序结束.它由 CDI 容器管理,与 EJB bean 完全无关.但是由于与 EJB 的互操作性,它可以被注入 (@Inject) 到 EJB @Singleton bean 中.EJB 规范和 CDI 规范中都没有要求 @Singleton bean 或 @ApplicationScope bean 可序列化.由于这是一个应用范围的实例,因此不需要钝化.

  1. CDI scopes are ideally context-of-usage-sensitive. @ApplicationScoped means that the CDI bean shall live from the instance it is created to the end of the application. It is managed by the CDI container and has absolutely nothing to do with EJB beans. But because of interoperability with EJB, it can be injected (@Inject) into an EJB @Singleton bean. There is no requirement in EJB spec, nor CDI spec that either @Singleton bean or @ApplicationScope bean be serializable. And since this is an application wide instance, it requires no passivation.

@SessionScope 使用当前容器希望的会话语义.例如,在 jsf 应用程序中,它通常会限定在 HttpSession 的生命周期内,但在没有 HttpSession 的 EJB 容器中,容器不会附加任何有意义的会话语义.它可能决定是每个 @Stateless 事务或它希望的任何事物.由于会话可以序列化,规范一般要求@SessionScoped bean 是可序列化的,但定义注入点的bean,如果不是@SessionScoped,则不需要可序列化.

@SessionScope uses whichever semantics of a session that current container wishes. for example in a jsf application, it will generally be scoped to the lifetime of the HttpSession, but in an EJB container without HttpSession, the container wont attach any meaningful session sematics. It might decide to be a per @Stateless transaction or anything it so wishes. Since sessions can be serialized, the spec generally requires that @SessionScoped beans be serializable, but the bean at which the injectionpoint is defined, if not @SessionScoped, need not be serializable.

@RequestScope 也遵循原子"动作单次执行的语义,例如http请求.在web容器中,一般会关联到HttpRequest,并没有要求可以序列化.在非 Web 上下文中,它可能与每次调用相关联,甚至与事务边界(在注入 EJB 的情况下)相关联,或者当无法归因于有意义的范围时,它将在注入点默认为 @Dependent 范围.

@RequestScope also follows the semantics of single execution of "atomic" action, e.g. httprequest. In a web container, it will generally be associated with HttpRequest, and there is no requirement that it be serializable. In a non-web context, it could be associated with a per invocation, or even transaction boundary (in case of injection into EJB) or it will default to @Dependent scope at the injection point, when no meaningful scope can be attributed.

也就是说,任何 CDI bean 都可以注入任何 EJB bean.通常在与 Web 容器无关的 EJB 容器中,@Dependent 和 @ApplicationScope 是仅有的具有任何有意义用法的作用域.

That said, any CDI bean can be injected into any EJB bean. Generally in an EJB container which is not associated with a web container, @Dependent and @ApplicationScope are the only scopes that would have any meaningful usages.

这篇关于JAVA EE CDI 范围、EJB 和托管 bean 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆