SETUP
Suppose that you have two bean archive modules A and B where A depends on B (we cannot modify B). Within B, suppose that there is a class in which an EntityManager is injected as follows:
@Dependent
class SomeClassImpl implements SomeClass {
private EntityManager em;
public SomeClassImpl() {}
@Inject
public SomeClassImpl(EntityManager em) {
this.em = em;
}
...
}
Note that SomeClass
interface is never directly injected into A. That is, SomeClass
is a dependency of SomeRepository
which in turn is a dependency of SomeUseCase
and this is what get injected in A.
Because the entity manager is not a cdi bean, we can define its producer within A. This works as expected when deploying in a WAR archive.
PROBLEM
Now, consider the following: you have three bean archive modules X, Y and Z where both X and Y depend on Z as defined above. Because X and Y define a producer for the EntityManager (different in X and Y) and these producers as visible to all modules, we must add qualifier to X and Y's producer to avoid the unsatisfied dependencies exception.
Now, the question is how do we deal with the ambiguity in Z?
If the producers were only visible to the module where defined, then this will be reduced the first scenario. But because they are not, we will get the org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers
Note: although this can be solved with custom scope, I would prefer to avoid building one if possible as of now.
If it were you, how would you go about solving this problem?