Skip to content
Advertisement

Two implementations of one interface in Jersey/HK2, reuse first in other

I have an interface with a naive implementation, say

JavaScript

I want to make a cache that caches the doIt, so I wrote a wrapper :

JavaScript

Then, I add both implementations to my Binder:

JavaScript

I get errors complaining about:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MyService,parent=CachingMyService},position=-1,optional=false,self=false,unqualified=null,1102650897)

I trief using Qualitifaction like this:

JavaScript

And using that:

JavaScript

But that does not work either.

What is the proper way to wrap a caching service like this? And how can I make hk2 choose the proper implementations?

Advertisement

Answer

You need to use the qualifiedBy(Annotation) method when you want to qualify different types. You also need to annotate each injection point with a different qualifier annotation.

First you need the annotations and have a way to get an instance of them (the qualifiedBy method requires an annotation instance)

JavaScript

Then when you bind them used qualifiedBy

JavaScript

Then when you inject them, add the applicable qualifier

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement