How to use with CDI? I am just getting WELD-001408: Unsatisfied dependencie

How to use with CDI? I am just getting WELD-001408: Unsatisfied dependencies for type ServiceRef<[OBJECTNAME]

Without the ServiceRef it works
This is what I am using

@Inject private ServiceRef <[OBJECTNAME]

jf;

I see there is a CdiServiceRef should I use that instead?

I managed to solve. Need to add the producer class to the beanmanager. Also ServiceRefProducer doesn’t work as the scope needs to be @Dependent. I managed to get it working with the following
@Produces
@Dependent
public ServiceRef serviceRefProd(InjectionPoint injectionPoint) {
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl typeImpl =
(ParameterizedTypeImpl) injectionPoint.getType();
Class serviceType = (Class) typeImpl.getActualTypeArguments()[0]
;
return CdiServiceRef.of(
serviceType,
injectionPoint
.getQualifiers()
.toArray(new Annotation[injectionPoint.getQualifiers().size()]
));
}

I’d like to fix this in the add-on, but I unfortunately don’t have a CDI setup to verify any fix.
From what I can see you did the following:

  1. Replace @ApplicationScoped with @Dependent.
  2. Fix the code to determine the service type. Is there a reason why you used ParameterizedTypeImpl instead of the interface java.lang.reflect.ParameterizedType?
  3. Anything else? Was my producer found at all?

If you’re willing, I can provide a SNAPSHOT build (in a Maven repository on Gitlab) for you to test.

ParameterizedTypeImpl
I just debugged what was being passed and used that. java.lang.reflect.ParameterizedType is probably better to use here.

Just by installing the add-on the producer is not found by default. I’m guessing its similar for Spring as the scanning would not be scanning de.codecamp.vaadin. I think the best way would be to provide the ServiceRefProducer as an interface with a default implementation and then users of the add-on implement the interface in their own ServiceRefProducer class in a package that is scanned.

For my Weld implementation I added

new Weld().addPackages(true, ServiceRefProducer.class)

Also note that the producer only works with @ANY or @DEFAULT qualifier

To add an additional qualifier I had to duplicate the producer with the additional qualifier. There must be a nicer way to do it!!

Spring Boot provides a mechanism to automatically configure an add-on when it has been added to the dependencies which does not require classpath scanning. It basically works similar to Java’s ServiceLoader.
I was under the impression that the beans.xml file would do something similar for CDI. But it seems [Weld does not yet support]
(https://stackoverflow.com/questions/30432295/cdi-bean-discovery-mode-annotated-is-ignored) the way I wrote it. But since the add-on’s archive is very small, we could try bean-discovery-mode="all" instead of bean-discovery-mode="annotated".

And Spring has the same issue: From the injection point it’s possible to determine the qualifiers, but once qualifiers are actually added to the injection point, the singleton producer/factory is no longer valid because it itself does not have those qualifiers. There does not seem to be a “catch all” qualifier.

I have uploaded a new version which should work out of the box for the @Default qualifier. You only need to declare your own producer methods to support other qualifiers. Tested with [skeleton-starter-flow-cdi]
(https://github.com/vaadin/skeleton-starter-flow-cdi).