Here is a very strange behavior using EJB beans:
@Local
public interface Provider {
void test();
}
@Local
public interface ExtProvider extends Provider {
void test2();
}
public abstract class AbstractProvider implements Provider {
@Override
void test(){ System.out.println("Hello strange " + getTech()); }
protected abstract String getTech();
}
public abstract class ExtAbstractProvider extends AbstractProvider implements ExtProvider {
@Override
void test2() { System.out.println("Hello from " + getName());}
@Override
String getTech() { return "extended EJB";}
protected abstract String getName();
}
@Stateless
public class ProviderBean extends AbstractProvider {
@Override
protected String getTech() { return "EJB";}
}
@Stateless
public class ExtProviderBean extends ExtAbstractProvider {
@Override
protected String getName() { return "ext provider";}
}
According to the above code, if I write:
@EJB Provider provider; // should inject an instance of ProviderBean
@EJB ExtProvider extProvider; // should inject an instance of ExtProviderBean
but no one of the two works!!! Someone would say that in this example the EJB doesn’t know which instance to create each time since there are two instances that implements Provider. And the strange is: it works only if we declare the two beans as:
public class ExtProviderBean extends ExtAbstractProvider implements ExtProvider
public class ProviderBean extends AbstractProvider implements Provider
In this case the code works. The problem is that we have to explicitly define that an implementation bean implements the interface even it is implicitly defined from the abstract implementation. Am I missing something or this is a limitation?
I just read through this SO question and I think the provided answer also applies to your case.
I am just citing a part of the answer and thereby the EE spec: