java - Prevent Managed Bean From Being Instantiated Multiple Times -
i'm new java sorry if terminology not correct.
i have managed bean setup (policyinformation) session scope instantiate in java code with:
thispolicy = (policyinformation) utils.getsessionmapvalue("policyinformationbean"); if (thispolicy == null) { thispolicy = new policyinformation(); }
the code above checks if bean exists, , if does, uses object in session map. seems work great.
however, if add label on xpage display value policyinformation managed bean following code (using expression language):
<xp:label value="#{policyinformationbean.name}"/>
the managed bean runs twice: once when call in java class, , again when add label above.
is there way can prevent second call when adding label on xpage? can somehow value java session map in xpage label?
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 3 things:
- try adding "id" property managed bean opening tag same name managed-bean-name. this not problem, how set 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>
remove code creates new policyinformation object. managed, have let framework manager time. think might confusing it, creating object.
alternatively, make bean pojo (plain old java object) , not managed bean @ all. way create object when need it. not have entry in faces-config anymore, , use 'new' keyword when need create it. when reference becomes null, garbage collection clean up.