In a Java EE 6, CDI 1.1.x, Seam 3 etc. environment, we need to find all CDI beans of the current view (@ViewScoped
). What I have tried so far is using:
@Named
@ViewScoped
public class ViewHelper
{
@Inject
private BeanManager beanManager;
public doSomethingWithTheBeanInstances()
{
Set<Bean<?>> beans = this.getBeanManager().getBeans(
Object.class, new AnnotationLiteral<Any>(){}
);
// do something
...
}
}
However, this returns all beans it manages.
I need to find only those within the scope of the current view and - that would be ideal - only those that implement a specific interface (inherited over over multiple hierarchy levels).
What's the way to do it?
Note since CDI has no view scope, we're using Seam 3 to be able to annotate all our view-scoped beans like:
@Named
@ViewScoped
public class ResultManagerColumnHandler extends BaseColumnHandler
{
....
}
The above would be an instance to look for (the @ViewScoped
is a CDI replacement by Seam 3).
How can it be done?
@Inject Instance<YourInterfaceType> instances
. Now, becauseInstance
implements Iterable, you should be able to iterate over all the instances. – OrdealContext context = beanManager.getContext(ViewScoped.class);
return a validContext
? (i.e. not null nor an exception). If that part works, then I can post an answer how to use it to get the currently active beans. – MelpomeneContext
would be valid, then it basically boils down to the approach as shown in his post. It has been in OmniFaces for ages (authored by yours truly). – Melpomene