[英]Prevent Managed Bean From Being Instantiated Multiple Times
I'm new to Java so sorry if my terminology is not correct. 我是Java新手,如果我的术语不正确,请对不起。
I have a managed bean setup (PolicyInformation) with a session scope that I instantiate in my Java code with: 我有一个托管bean设置(PolicyInformation),其会话范围是在我的Java代码中使用以下方法实例化的:
thisPolicy = (PolicyInformation) Utils.getSessionMapValue("policyInformationBean");
if (thisPolicy == null) {
thisPolicy = new PolicyInformation();
}
The code above checks if the bean already exists, and if it does, it uses the object in the session map. 上面的代码检查Bean是否已经存在,如果存在,则使用会话映射中的对象。 It seems to work great.
看起来效果很好。
However, if I add a label on my XPage to display a value from my PolicyInformation managed bean with the following code (using expression language): 但是,如果我在XPage上添加标签以使用以下代码(使用表达式语言)显示来自PolicyInformation托管bean的值:
<xp:label value="#{policyInformationBean.name}"/>
the managed bean runs twice: once when I call it in my Java class, and again when I add the label above. 托管bean运行两次:一次是在Java类中调用它,另一次是在上面添加标签时。
Is there a way I can prevent that second call when adding the label on my XPage? 在我的XPage上添加标签时,有什么方法可以防止再次调用吗? Can I somehow get the value from the Java session map in my XPage label?
我可以以某种方式从XPage标签中的Java会话映射中获取值吗?
faces-config: faces-config:
<managed-bean>
<managed-bean-name>policyInformationBean</managed-bean-name>
<managed-bean-scope>session</managed-bean-scope>
<managed-bean-class>com.package.PolicyInformation</managed-bean-class>
</managed-bean>
Please consider these three things: 请考虑以下三件事:
Example: 例:
<managed-bean id="dBar">
<managed-bean-name>dBar</managed-bean-name>
<managed-bean-class>eu.linqed.debugtoolbar.DebugToolbar</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Remove all code that creates a new PolicyInformation object. 删除所有创建新PolicyInformation对象的代码。 To be managed, you have to let the framework manager it all the time.
要进行管理,您必须一直让框架管理器负责。 I think you might be confusing it, by creating another object.
我认为通过创建另一个对象,您可能会感到困惑。
Alternatively, you could make this bean a POJO (Plain Old Java Object) and not a managed bean at all. 另外,您可以使此bean成为POJO(普通的旧Java对象)而不是托管的bean。 This way you just create the object when you need it.
这样,您就可以在需要时创建对象。 You would not have an entry in faces-config anymore, and you would use the 'new' keyword when you need to create it.
您将不再在faces-config中拥有任何条目,并且需要创建时使用“ new”关键字。 When the reference becomes null, then garbage collection will clean it up.
当引用变为null时,垃圾收集将对其进行清理。
Anyone at IBM, please have a look at the following Classes: IBM的任何人,请查看以下课程:
Thread [Thread-7] (Suspended (breakpoint at line 131 in ApplicationController))
线程[Thread-7](挂起(在ApplicationController中的第131行的断点))
ApplicationController.() line: 131ApplicationController。()行:131
J9VMInternals.newInstanceImpl(Class) line: not available [native method]J9VMInternals.newInstanceImpl(Class)行:不可用[本机方法]
Class.newInstance() line: 1688Class.newInstance()行:1688
Beans .instantiate(ClassLoader, String, BeanContext, AppletInitializer) line: 189
豆 .instantiate(类加载器,字符串,BeanContext中,AppletInitializer)线:189
Beans.instantiate(ClassLoader, String) line: 80 ManagedBeanFactory$1.run() line: 222Beans.instantiate(ClassLoader,String)行:80 ManagedBeanFactory $ 1.run()行:222
AccessController.doPrivileged(PrivilegedExceptionAction) line: 413AccessController.doPrivileged(PrivilegedExceptionAction)行:413
ManagedBeanFactory .newInstance(FacesContext) line: 216ManagedBeanFactory .newInstance(FacesContext)行:216
ApplicationAssociate.createAndMaybeStoreManagedBeans(FacesContext, String) line: 269ApplicationAssociate.createAndMaybeStoreManagedBeans(FacesContext,String)行:269
VariableResolverImpl .resolveVariable(FacesContext, String) line: 135 VariableResolverImpl.resolveVariable(FacesContext, String) line: 71
VariableResolverImpl .resolveVariable(FacesContext,String)行:135 VariableResolverImpl.resolveVariable(FacesContext,String)行:71
In my opinion IBM is not able to instanciate a application, view or session scoped variable synchronized, even thought they <sarcasm> allready have </sarcasm> JAVA 1.6 我认为IBM无法实例化应用程序,视图或会话范围内的变量是否同步,甚至认为它们<sarcasm>已经准备好</ sarcasm> JAVA 1.6
http://www-01.ibm.com/support/docview.wss?uid=swg21188789 allready wondering how long this link will work... http://www-01.ibm.com/support/docview.wss?uid=swg21188789已经准备 好想知道此链接可以使用多长时间...
Please consider race conditions as a real thing, eg for instanciating. 请考虑比赛条件是真实的事情,例如,为了鼓舞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.