I'm trying to implement some kind of producer factory pattern.
Is it somehow possible to trigger the producer method of a base type while injecting a derived type?
Assuming following interfaces:
interface Service
interface AService extends Service
I want to trigger this producer:
@Produces
Service factory()
At this injection point:
@Inject
AService srv;
The purpose is to have one producer factory for different kinds of services by adding a marker interface (Service in this case).
Thanks for helping me
Update:
I tried LightGuards solution and added @Typed
to the AService implementation:
@Typed(Service.class)
class AServiceImplemenation implements AService
Unfortunately I get an unsatisfied dependencies error. CDI does't invoke the Service
producer for the AService
injection point. Certainly, this make sense for typesafty reasons. But is there a way to force the invocation of the Service
producer?
If you only have a producer for the super type, and make it so that the AService type doesn't have a no args ctor (or one annotated with @Inject
), or is @Typed
, or @Vetoed
in CDI 1.1 you could return any subclass of Service
from your producer.