java - @Inject create a new bean instance in the session -
it similar @inject injecting new instance every time use it, cannot find answer in thread.
i'm new both cdi , jsf, , i'm trying use cdi instead of jsf annotations. want retrieve credential membercontroller. bean (both) invoked jsf pages. problem instance of credential in membercontroller has null name/password, confirmed setter in credential hit. don't understand why there 2 instances of credential. want through @managedbean+@managedproperty. want know how same thing cdi.
my environment jboss 7.1.1+java ee 6
credential.java
import javax.enterprise.context.sessionscoped; import javax.inject.named; import java.io.serializable; @named @sessionscoped public class credential implements serializable{ private static final long serialversionuid = 680524601336349146l; private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } private string password; }
membercontroller.java
@named @sessionscoped public class membercontroller implements serializable { private static final long serialversionuid = 8993796595348082763l; @inject private credential newcredential; public void login() { string msg = newcredential.getname()+":"+newcredential.getpassword(); } }
jsf page segment
<p:inputtext id="username" label="username" value="#{credential.name}" /> <p:password id="password" label="password" value="#{credential.password}" /> <p:commandbutton value="login" action="members" actionlistener="#{membercontroller.login}" />
the injection doesn't call new instance (well first time it's been called, or injected bean has requestscope). depending on scope use bean. scope use generate each person using application new bean (their own sessionscope bean). data inside scope visible person. if want global container application accessible users of application, containing same data everyone, should create applicationscope or singelton bean. annotations create 1 container accessible during lifetime of application, central point holding data.
Comments
Post a Comment