使特定的jsf bean会话无效 [英] Invalidate specific jsf bean session

查看:118
本文介绍了使特定的jsf bean会话无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使会话中的特定bean无效?

How I invalidate a specific bean in a session?

我有这个示例代码。
我使用ExternalContext.invalidateSession()进行测试;但它会破坏应用程序中会话中的所有bean,因为它会破坏整个会话。

I have this example code. I test with ExternalContext.invalidateSession(); but it destroy all beans in the session in the application since it destroys the full session.

@Named
@SessionScoped
class Managed implements Serializable {

       public void invalidate (){
           // lines //
           externalContext.invalidateSession();   
       }

}

但是,使用invalidateSession所有bean都在会话被销毁,我想只使一个特定的托管bean无效,我是怎么做的?

but, with invalidateSession all beans in the session are destroyed, I want to invalidate only the one specific "Managed" bean, how I do that?

推荐答案

忽略事实上,你不清楚如何你想要实现这个解决方案,开始

Ignoring the fact that you're not clear on how you want to implement this solution, to start


  1. 注入 BeanManager 进入您计划执行逻辑的任何地方。 是一个托管组件

@Inject
BeanManager beanManager;

bean管理器是允许您访问所有CDI bean(以及其他东西)的组件在你的上下文中。

The bean manager is the component that will grant you access to all the CDI beans (and other stuff) within your context.

然后使用 BeanManager 获取对你的bean的上下文引用对此感兴趣

You then use the BeanManager to get a contextual reference to the bean you're interested in

Bean<Managed> bean = (Bean<Managed>) beanManager.resolve(beanManager.getBeans(Managed.class));
Managed managedBean= (Managed) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean)

managedBean = null; //or whatever you want to do with it


此解决方案应销毁该活动的实例会话bean;如果再次尝试使用同一个bean,CDI很可能会按需创建一个全新的实例

This solution should destroy the active instance of that session bean; if another attempt is made to use that same bean, CDI will most likely create a brand new instance on-demand

这篇关于使特定的jsf bean会话无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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