0

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?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Eric B.
  • 23,425
  • 50
  • 169
  • 316

1 Answers1

2

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

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • Thanks. I tried to read up on the `@Typed` annotation but I don't exactly understand how I can leverage that in my case. I'm trying to retrieve the bean programmatically, so I wouldn't be annotating a field with that annotation. So I need to pass it as an annotation to the `getBeans()` call? Ie: bm.getBeans(com.pkg.MyClass.class, Typed.class) – Eric B. Jan 10 '18 at 02:32
  • No, you would annotate the implementation with `@Typed` so that it is only resolvable as those specific type(s). – John Ament Jan 10 '18 at 02:33