Search code examples
javajakarta-eeejb

Order of EJB injection lookup for multiple possibilities


Having multiple implementations of a @Local Interface:

@Local
public interface LocalInterface {
}

@Stateless
public class FirstLocalImpl implements LocalInterface {
}

@Stateless
public class SecondLocalImpl implements LocalInterface {
}

and an @EJB Injection in a third bean:

public class Foo {
   @EJB
   LocalInterface local;
}

Which implementation is chosen to be injected into Foo::local and how is the order defined? Note that no ejb-jar.xml is specified.


Solution

  • You will get a detailed error at deployment time, telling you that there are two implementations of the same interface. To solve this you have to specify the bean name:

    @EJB(beanName="FirstLocalImpl")
    LocalInterface local;