문제

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