2014-05-07 38 views
1

我有如下定義一個CDI豆:如何刪除CDI會話範圍的bean

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

    private String username; 
    private String password; 

現在我想從程序會話範圍中刪除:

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

ins變量始終爲空。如何檢索loginBean實例並銷燬它?

+0

[How to invalidate session in JSF 2.0?](http://stackoverflow.com/questions/5619827/how-to-invalidate-session-in-jsf-2-0) – mabi

+0

此外,規範鏈接關於範圍:http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – mabi

回答

3

您不能安全銷燬CDI bean而不會破壞某些東西。它應該在上下文結束時自動銷燬(在這種情況下,當會話過期或失效時)。
如果您希望破壞發生時將範圍縮小到ConversationScoped(並設置邊界)或ViewScopedavailable in JSF 2.2,當用戶導航到另一個視圖時,bean將被銷燬)。