简体   繁体   English

JAVA EE CDI范围,EJB和托管Bean序列化

[英]JAVA EE CDI Scopes, EJBs and managed beans serialization

Have some questions regarding scopes, ejbs, and managed beans. 对范围,ejb和托管bean有一些疑问。

  1. Are scopes (javax.enterprise.context.ApplicationScope, javax.enterprise.context.SessionScope) only for EJBs? 范围(javax.enterprise.context.ApplicationScope,javax.enterprise.context.SessionScope)仅适用于EJB吗? Or are they for all managed beans? 还是它们适用于所有托管bean? Until today I was pretty sure it was for all managed beans. 直到今天,我仍然确信它适用于所有托管bean。
  2. In my application we have: 在我的应用程序中,我们有:

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

    and some manager: 和一些经理:

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

    and a web service: 和网络服务:

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

The container (deploy time) or compiler does not complain about it, is that normal? 容器(部署时间)或编译器没有抱怨,这正常吗?

I thought that: 我认为:

"Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable." “使用会话,应用程序或会话范围的Bean必须可序列化,但是使用请求范围的Bean不必是可序列化的。” JAVA EE Using Scopes JAVA EE使用范围

MyClass should implement Serializable or not? MyClass是否应该实现Serializable? Could we say that because the managed bean is injected into a @Singleton, serialization never occurs? 我们是否可以说因为将托管bean注入到@Singleton中,所以序列化永远不会发生? Therefore no serialization error is shown at deploy time? 因此在部署时没有显示序列化错误吗?

  1. If Yes: If I make MyClass @ApplicationScoped and @Stateful and inject it in SomeManager using @EJB, than at deployment time I do get an error about serialization.. 如果是:如果我使MyClass @ApplicationScoped和@Stateful并使用@EJB注入SomeManager,则在部署时确实会出现有关序列化的错误。
  2. If No: Why don't I get some NullPointerExceptions for the transient Logger (due to pasivitation/activation)? 如果否:为什么我不会为临时Logger收到一些NullPointerExceptions(由于无聊/激活)?

CDI scopes are evaluated in the context of CDI container. CDI范围是在CDI容器的上下文中评估的。 That said, the designers of CDI specification ensured that it is operable with EJB and jsf Managed Bean. 也就是说,CDI规范的设计者确保了它可以与EJB和jsf Managed Bean一起使用。 That said. 那就是。

  1. CDI scopes are ideally context-of-usage-sensitive. CDI范围在理想情况下是上下文相关的。 @ApplicationScoped means that the CDI bean shall live from the instance it is created to the end of the application. @ApplicationScoped表示CDI bean从创建实例到应用程序末尾都应存在。 It is managed by the CDI container and has absolutely nothing to do with EJB beans. 它由CDI容器管理,与EJB Bean绝对无关。 But because of interoperability with EJB, it can be injected (@Inject) into an EJB @Singleton bean. 但是由于与EJB的互操作性,可以将其注入(@Inject)到EJB @Singleton bean中。 There is no requirement in EJB spec, nor CDI spec that either @Singleton bean or @ApplicationScope bean be serializable. EJB规范或CDI规范都没有要求@Singleton bean或@ApplicationScope bean可序列化。 And since this is an application wide instance, it requires no passivation. 并且由于这是一个应用程序范围的实例,因此不需要钝化。

  2. @SessionScope uses whichever semantics of a session that current container wishes. @SessionScope使用当前容器希望的会话语义。 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. 例如,在jsf应用程序中,通常将其范围限制为HttpSession的生存期,但是在没有HttpSession的EJB容器中,该容器将不会附加任何有意义的会话语义。 It might decide to be a per @Stateless transaction or anything it so wishes. 它可能决定是每个@Stateless事务或它希望执行的任何操作。 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. 由于会话可以序列化,因此规范通常要求@SessionScoped bean是可序列化的,但是定义注入点的bean(如果不是@SessionScoped)则不需要序列化。

  3. @RequestScope also follows the semantics of single execution of "atomic" action, eg httprequest. @RequestScope还遵循“原子”动作(例如httprequest)的单次执行的语义。 In a web container, it will generally be associated with HttpRequest, and there is no requirement that it be serializable. 在Web容器中,通常将其与HttpRequest关联,并且不需要使其可序列化。 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. 在非Web上下文中,它可能与每次调用甚至事务边界(在注入到EJB的情况下)相关联,或者在无法分配有意义的作用域的情况下,它将在注入点默认为@Dependent作用域。

That said, any CDI bean can be injected into any EJB bean. 也就是说,任何CDI bean都可以注入到任何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. 通常,在与Web容器不关联的EJB容器中,@ Dependent和@ApplicationScope是仅有的具有任何有意义用法的作用域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM