How to control OSGi dependency injection in AEM?
I'm trying to understand how dependency injection works in OSGi (specifically Apache Felix as used in AEM). I have a servlet with an `@Reference` annotation on a field that references an interface -- in my case, it represents a secure document signing provider. I have an implementation class that implements the interface, and it's automatically injected into the servlet.
In the servlet:
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
private DocumentSigningProvider signingProvider;
...
URL redirectUrl = signingProvider.Sign(...);
and my implementation class:
@Component(metatype=true)
@Service
public class DocumentSigningDocuSignImpl implements DocumentSigningProvider {
...
@Override
public URL Sign(...) {...}
As you can see, in my servlet I code against the interface, not the implementation, which is never mentioned in the servlet.
Question: When I write a 2nd implementation class for the same interface, how do I control which implementation is injected into the servlet?
I'd like to control this via configuration so I can use a different impl class in different run modes. Secondarily, it might be useful to be able to select the impl dynamically at runtime, e.g if I need to select among implementations based on some request parameter.