Question

I have a CDI bean defined as following:

@Named("loginBean")
@SessionScoped
public class LoginBean implements Serializable {
    @EJB
    private LoginManager loginManager;

    private String username;
    private String password;

Now I want to remove it from session scope programmatically:

HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
LoginBean ins = (LoginBean)session.getAttribute("loginBean");

But the ins variable is always null. How can I retrieve loginBean instance and destroy it?

Était-ce utile?

La solution

You cannot safely destroy a CDI bean without breaking something. It is supposed to be destroyed automatically when its context ends (in this case when the session is expired or invalidated).
If you want destruction to happen earlier narrow the scope down to ConversationScoped (and set its boundaries) or ViewScoped (available in JSF 2.2, the bean will be destroyed when a user navigates to another view).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top