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

查看:113
本文介绍了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不必可序列化." 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. 如果否:为什么我不会为临时Logger收到一些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也遵循原子"动作的单次执行的语义,例如httprequest.在Web容器中,通常将其与HttpRequest关联,并且不要求它可序列化.在非Web上下文中,它可能与每次调用甚至事务边界(在注入到EJB的情况下)相关联,或者在无法分配有意义的范围的情况下,它将默认为@Dependent范围在注入点. /p>

@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天全站免登陆