質問

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?

役に立ちましたか?

解決

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).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top