Java – two EJBs with the same interface Is this a good habit?
My use case requires me to have a class hierarchy, as shown below
public interface ServiceA{ public void doSomething();} public abstract class AbstractClass implements ServiceA{ @Override public void doSomething(){ getMetaValue(); .. do common Things applicable to all EJBS... } public abstract MeataValue getMetaValue(); } @Stateless(mappedName="EJBBeanImlJ") public EJBBeanImplJ extends AbstractClass{ public MetaValue getMetaValue(){ return new MetaValue(x,y); } } @Stateless(mappedName="EJBBeanImplK") public EJBBeanImplK extends AbstractClass{ public MetaValue getMetaValue(){ return new MetaValue(a,b); } }
Question:
>Is it a good EJB practice to provide the same interface for two EJB implementations? > Do you see any other shortcomings in the class design / hierarchy?
Note: my app server is Weblogic
thank you
Solution
EJB is just a special class It is special because its lifecycle is managed by the container It is just a class, because it is written in Java and can implement any interface and any business logic it wants
Therefore, I think it is a good habit to have multiple implementations of the same interface It allows the implementation of a particular service to be separated from its use For example, you can create an interface sender that can send some content and two implementations: emailsender and smssender Both implement the same interface, both EJB
The only problem is that in this case, you can't just bind the reference to sender through its interface, but you must use mappedname. Com as you do