简体   繁体   English

防止托管Bean多次实例化

[英]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: 请考虑以下三件事:

  1. Try adding an "id" property to your managed bean opening tag with the same name as your managed-bean-name. 尝试将“ id”属性添加到托管bean的开始标记中,该属性与托管bean的名称相同。 This is likely not your problem, but this is how we set it up. 这可能不是您的问题,但这是我们设置的方式。

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>
  1. 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. 我认为通过创建另一个对象,您可能会感到困惑。

  2. 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: 131 ApplicationController。()行:131
J9VMInternals.newInstanceImpl(Class) line: not available [native method] J9VMInternals.newInstanceImpl(Class)行:不可用[本机方法]
Class.newInstance() line: 1688 Class.newInstance()行:1688

Beans .instantiate(ClassLoader, String, BeanContext, AppletInitializer) line: 189 .instantiate(类加载器,字符串,BeanContext中,AppletInitializer)线:189
Beans.instantiate(ClassLoader, String) line: 80 ManagedBeanFactory$1.run() line: 222 Beans.instantiate(ClassLoader,String)行:80 ManagedBeanFactory $ 1.run()行:222
AccessController.doPrivileged(PrivilegedExceptionAction) line: 413 AccessController.doPrivileged(PrivilegedExceptionAction)行:413
ManagedBeanFactory .newInstance(FacesContext) line: 216 ManagedBeanFactory .newInstance(FacesContext)行:216
ApplicationAssociate.createAndMaybeStoreManagedBeans(FacesContext, String) line: 269 ApplicationAssociate.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.

相关问题 在Spring MVC中多次实例化Singleton bean? - Singleton bean instantiated multiple times in Spring MVC? 托管应用程序范围的Bean创建多次 - Managed Application scoped bean creates multiple times 使用什么异常来防止方法被多次调用? - What exception to use to prevent a method from being called multiple times? 类被多次实例化; 如何分辨正在呼叫哪一个? - Class instantiated multiple times; how to tell which one is being called? 在JPA中,有没有一种方法可以防止对象被多次实例化? - In JPA, is there a way to prevent an object from being instantiated more than once? 查看多次调用Scoped Bean preRenderView方法 - View Scoped Bean preRenderView method being called multiple times 防止井字游戏中多次单击同一按钮 - Prevent same button from being clicked multiple times in tic tac toe game 如何防止 singleton bean 被 ApplicationContext 刷新破坏 - How to prevent a singleton bean from being destroyed by the ApplicationContext refresh 防止在Spring Cloud Bootstrap上下文中构造bean - Prevent bean from being constructed in the spring cloud bootstrap context Android:我可以阻止Java类(来自外部库)在运行时实例化 - Android: can I prevent a Java class (from an external library) from being instantiated at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM