从会话中删除bean [英] Removing beans from session
问题描述
我有一些 @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).
我正在执行以下操作,以删除一些在用户成功下达了在线订单。
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会话无关,在该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< String,Object>
。
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?
UPDATE:
请记住,一旦将bean绑定到上下文,它将保留在
中直到上下文被破坏为止。 无法通过
从上下文中手动删除bean。如果您不希望将
的bean无限期地放置在会话中,请考虑将另一个作用域与$一起使用b $ b缩短了寿命,例如请求或对话范围。
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引入了 AlterableContext
接口, destroy(Bean< T>)
方法。
CDI 1.1 introduced an AlterableContext
interface with a destroy(Bean<T>)
method.
通过 beanManager.getContext(SessionScoped.class)
获取会话上下文,向下转换为 AlterableContext
,然后使用适当的bean类型调用 destroy()
。
Get the session context via beanManager.getContext(SessionScoped.class)
, downcast to AlterableContext
and then invoke destroy()
with the appropriate bean type.
这篇关于从会话中删除bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!