删除/销毁会话范围的CDI托管bean [英] Remove/destroy session scoped CDI managed bean

查看:128
本文介绍了删除/销毁会话范围的CDI托管bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个会话范围的CDI托管bean:

I have a session scoped CDI managed bean:

@Named
@SessionScoped 
public class SampleBean implements Serializable {
    // ...
}

我需要在一定流程之后从会话中删除此bean,为此我使用了以下代码,如

I need to remove this bean from the session after a certain flow for which I used the following code like as seen in this answer:

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getSessionMap().remove("sampleBean");

但是,它不起作用,并且SampleBean保留在会话中.
我想念什么吗?

However, it does not work and the SampleBean remains in the session.
Am I missing something?

推荐答案

与JSF托管Bean相反,CDI托管Bean并非直接通过其托管Bean名称直接存储在会话映射中.相反,它们由CDI管理器实现(例如Weld,OpenWebBeans等)存储在服务器的内存中,例如会话ID作为键.

In contrary to JSF managed beans, CDI managed beans are not stored directly by their managed bean name in the session map. They are instead stored in server's memory by the CDI manager implementation (Weld, OpenWebBeans, etc) using e.g. session ID as key.

因此,您在此处使用的技巧不适用于CDI管理的bean.您需要寻找一种替代方法.在这种情况下,正确的方法是使用@ConversationScoped而不是@SessionScoped.在设计正确的Web应用程序中,永远不需要手动终止作用域.因此,首先使用@SessionScoped进行对话/流程是错误的.

The trick which you used there is therefore not applicable on CDI managed beans. You need to look for an alternate approach. The right approach in this particular case is to use @ConversationScoped instead of @SessionScoped. In properly designed web applications, there should never be the need to manually terminate a scope. So using @SessionScoped for a conversation/flow was already wrong in first place.

这篇关于删除/销毁会话范围的CDI托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆