Eric B.
Eric B.

Reputation: 24411

How to retrieve a bean by classname from CDI BeanManager? (an not any children)

I'm trying to understand a snippet of code and don't understand the documentation for CDI BeanManager

Bean resolve(Set> beans) Apply the ambiguous dependency resolution rules to a set of beans.

What are the ambiguous dependency resolution rules? How does CDI reduce a Set<Bean> beans to a single Bean?

I'm running into a slight issue where I want to look up a bean by exact type, but not sure how to do it:

    Set<Bean<?>> beans = bm.getBeans(com.pkg.MyClass.class, annotations);
    Bean<?> bean = bm.resolve(beans);

will find my all beans of type com.pkg.MyClass - both the exact implementation and any children. If only children exist in the CDI, then it will return one of the children. However, I only want to find the exact com.pkg.MyClass bean. If not the exact bean isn't there, I want to return null or throw an exception.

How can tell the bean manager that I only want an exact match when searching for a bean by classname?

Upvotes: 0

Views: 377

Answers (1)

John Ament
John Ament

Reputation: 11723

Typically to do this, you would annotate the actual implementation with @Typed and specify only that type. See also https://docs.jboss.org/cdi/api/1.0/javax/enterprise/inject/Typed.html

Upvotes: 2

Related Questions