java ee - PostConstruct in CDI SessionScoped managed beans -


short story: have cdi @sessionscoped bean (annotation javax.enterprise.context , not javax.faces) called usercontextbean. i'd when http session created.

so naturally assumed @postconstruct trick: called once when instance of bean constructed. however, per documentation here, postconstruct method called :

when managed bean injected component, cdi calls method after injection has occurred , after initializers have been called.

i had assumed post construct method called once, per session. however, practice consistent documentation. bean injected @requestscoped bean (also cdi) serves backing bean jsf page , postconstruct method called for every request.

i realize (now) behavior. there other way of doing 1 time per session initialization?

some code, although not relevant:

@named(usercontextbean.bean_name) @sessionscoped public class usercontextbean implements serializable {  ...      @postconstruct     private void createsession() {         system.out.println("usercontext created.");     } } 

the request scoped bean inject this:

public abstract class webpagedataprovider extends abstractviewdataprovider {      @inject     private usercontextbean usercontext; 

i thinking use httpsessionlistener , initialize session bean, sounds messy.

edit

just noticed http session not created. if create session "manually", calling getsession(true) (i inside phase listener, testing purposes, because there) works expected.

an httpsessionlistener best bet. inject sessionscoped component listener , set value there.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -