从会话中删除特定的 CDI 托管 bean [英] Removing specific CDI managed beans from session

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

问题描述

我有一些 @SessionScoped CDI bean.以前都是JSF托管bean(从JSF托管bean改为CDI托管bean).

I have some @SessionScoped CDI beans. Previously all of them were JSF managed beans (changed from JSF managed beans to CDI managed beans).

我正在执行以下操作,以删除在用户成功下在线订单后管理的某些会话范围的 JSF.

I was doing like the following to remove some session scoped JSF managed after a user successfully places an online order.

Map<String, Object> sessionMap = context.getSessionMap();

sessionMap.remove("cartBean");
sessionMap.remove("orderItems");
sessionMap.remove("reviewOrderBean");
sessionMap.remove("intermediateLocaleBean");
sessionMap.remove("localeBean");
sessionMap.remove("currencyRateBean");
sessionMap.remove("currency");

以便在订单处理完成后将所有内容设置为默认值.这与破坏/无效整个 HTTP 会话无关,其中在订单处理成功终止后用户不得注销.

So that everything is set to default after order processing completes. This is irrelevant to destroying/invalidating the entire HTTP session where the user must not be logged out after order processing terminates successfully.

与 JSF 托管 bean 不同,CDI bean 由一些 CDI 管理器实现(如 Weld)存储到服务器的内存中.因此,它们在会话映射中不可用 - Map.

Unlike JSF managed beans, CDI beans are stored into server's memory by some CDI manager implementation like Weld. Therefore, they are not available in a session map - Map<String, Object>.

对于 CDI bean,这无论如何如何模拟?

How can this be simulated anyway regarding CDI beans?

更新:

焊接文档 :

请记住,一旦 bean 绑定到上下文,它就会保留在该上下文直到上下文被销毁.没有办法从上下文中手动删除 bean. 如果您不希望 bean无限期地坐在会话中,考虑使用另一个范围生命周期缩短,例如请求或对话范围.

Keep in mind that once a bean is bound to a context, it remains in that context until the context is destroyed. There is no way to manually remove a bean from a context. If you don't want the bean to sit in the session indefinitely, consider using another scope with a shorted lifespan, such as the request or conversation scope.

所以我假设,很难在不破坏其他东西的情况下手动销毁 bean.

So I assume, it is hard to manually destroy a bean without destroying something else.

推荐答案

CDI 1.1 引入了带有 destroy(Bean) 方法的 AlterableContext 接口.

CDI 1.1 introduced an AlterableContext interface with a destroy(Bean<T>) method.

通过beanManager.getContext(SessionScoped.class)获取会话上下文,向下转换为AlterableContext,然后使用适当的调用destroy()豆类.

Get the session context via beanManager.getContext(SessionScoped.class), downcast to AlterableContext and then invoke destroy() with the appropriate bean type.

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

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