Removing specific CDI managed beans from session
Solution 1:
CDI 1.1 introduced an AlterableContext
interface with a destroy(Bean<T>)
method.
Get the session context via beanManager.getContext(SessionScoped.class)
, downcast to AlterableContext
and then invoke destroy()
with the appropriate bean type.
Solution 2:
Just to clarify Harald's answer:
@Inject
BeanManager beanManager;
.....
AlterableContext ctxSession = (AlterableContext) beanManager.getContext(SessionScoped.class);
for (Bean<?> bean : beanManager.getBeans(YourSessionBeanToBeDestroyedClass.class)) {
Object instance = ctxSession.get(bean);
if (instance != null)
ctxSession.destroy(bean);
}