Search code examples
javajakarta-eeejbejb-3.0

How do I 'narrow' down to a specific EJB when there are multiple EJB's


I have the following situation:

Two EJB's implementing the same interface

@Stateless(name="theOne")
public class EjbOne implements InterfaceOne {
    ...
}

@Stateless(name="theSecond")
public class EjbTwo implements InterfaceOne {
    ...
}

I want to inject a specific EJB into a third class

@Stateless
public class EjbThree  {
    @EJB(...???...mappedName="theOne"? name=...? )
    private EjbOne ejbOne;
}

How should I do this? (Mind, I'm using @EJB, not @Inject with a @Qualifier)


Solution

  • You are referencing concrete class private EjbOne ejbOne; There is no alternative. You are not using interface at all instead you should reference like this if you want use good old polymorphism @EJB private InterfaceOne ejbOne then your question will make sense, what you can do is:

    a. @EJB(beanName="EjbOne") private InterfaceOne ejbOne

    b. @EJB(lookup ="jndi-address") private InterfaceOne ejbOne

    c. @EJB(mappedName="vendor-specific-address") private InterfaceOne ejbOne

    d. @EJB(name="java:comp/env like logical name") private InterfaceOne ejbOne

    https://docs.oracle.com/javaee/7/api/javax/ejb/EJB.html