gpt4 book ai didi

jsf - 从 session 中删除特定的 CDI 托管 bean

转载 作者:行者123 更新时间:2023-12-05 01:21:03 25 4
gpt4 key购买 nike

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

我正在执行以下操作,以删除在用户成功下在线订单后管理的一些 session 范围的 JSF。

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 session 无效无关,其中在订单处理成功终止后用户不得注销。


与 JSF 管理的 bean 不同,CDI bean 由某些 CDI 管理器实现(如 Weld)存储到服务器的内存中。因此,它们在 session 映射中不可用 - Map<String, Object> .

关于 CDI bean 如何模拟?


更新:

The Weld documentation :

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。

最佳答案

CDI 1.1 引入了一个 AlterableContextdestroy(Bean<T>) 的接口(interface)方法。

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

关于jsf - 从 session 中删除特定的 CDI 托管 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com