EJB是否为实例变量,并在JSF Managed Bean中标记为transient? [英] Should EJBs be instance variables and marked as transient in JSF Managed Beans?
问题描述
有效地,从JSF Managed Bean中,我们可以通过从ServletContext获取我们希望使用的EJB来查找EJB接口。
所以问题是:
1)使EJB接口成为一个实例变量ManagedBean? (原因是EJB在一个页面循环期间被多次调用)
2)如果我们做它们的实例变量,我们应该将EJB Interfaces实例变量标记为暂时性?
1)使EJB接口成为一个实例变量在ManagedBean? (原因是EJB在一个页面循环期间被多次调用)
这是正常的设计,是的。使用 @EJB
时并没有什么不同。返回的EJB实例是代理。代理人也将担心将方法调用委托给容器的EJB池中适当且可用的具体EJB实例。
您唯一关心的可能是 @Stateless
与 @Stateful
相对于JSF托管的bean范围。您需要真正了解每个EJB会话代表什么。 @Stateless
可能会在每次通话时返回一个随机实例。只要客户端(在这种特殊情况下,JSF托管的bean实例)生活, @Stateful
可以提供相同的实例。可以在这里找到更深入的解释: JSF请求作用域bean每次请求时都会重新创建新的有状态会话bean?
< blockquote>
2)如果我们做出实例变量,我们应该将EJB Interfaces实例变量标记为transient?
不需要。 EJB代理默认情况下是可序列化的。
We have several JSF Managed beans with Request, View, and Session Scope and are running WebLogic 11g (10.3.2). Weblogic does not support the @EJB annotation in a JSF Managed Bean, so we have used these procedures http://technology.amis.nl/2008/12/06/ejb-dependency-injection-of-session-bean-facade-in-jsf-12-on-weblogic-103-jsf-with-jpa/ to create a ServletConextListener to load EJB references using the @EJB annotation.
Effectively, from within the JSF Managed Bean, we are then able to look up the EJB interface for the EJB that we wish to use by getting it from the ServletContext.
So the questions are:
1) Is it OK to make an EJB interface an instance variable on a ManagedBean? (the rationale is that the EJB is called many times during a page cycle)
2) if we do make them instance variables, should we mark the EJB Interfaces instance variables as transient?
1) Is it OK to make an EJB interface an instance variable on a ManagedBean? (the rationale is that the EJB is called many times during a page cycle)
That's the normal design, yes. It's not different from when using @EJB
. The returned EJB instance is a proxy anyway. The proxy will in turn worry about delegating the method calls to the proper and available concrete EJB instance in the EJB pool of the container.
Your only concern may be @Stateless
versus @Stateful
as seen relative to the JSF managed bean scope. You need to really understand what each the EJB session stands for. The @Stateless
may return you a random instance on every call. The @Stateful
gives you the same instance as long as the client (in this particular case, the JSF managed bean instance) lives. A more in depth explanation can be found here: JSF request scoped bean keeps recreating new Stateful session beans on every request?
2) if we do make them instance variables, should we mark the EJB Interfaces instance variables as transient?
Not needed. The EJB proxy is by default already serializable.
这篇关于EJB是否为实例变量,并在JSF Managed Bean中标记为transient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!