Reputation: 1949
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);
}
}
Questions :
Is it a good EJB practice to have same interface for two EJB implementations ?
Do you see any other drawbacks in class design/hierarchy ?
Note : My appserver is Weblogic
Thanks
Upvotes: 4
Views: 4457
Reputation: 115328
EJB is just a kind of special class. It is special because its life cycle is managed by container. It is just a class because it is written in java and may implement any interface and any business logic it wants.
So, I think that it is a good practice to have several implementations of the same interface. It allows separation of implementation of specific service from its usage. For example you can create interface Sender
that can send some content and 2 implementations: EmailSender and SmsSender. Both implement the same interface, both are EJBs.
The only problem is that in this case you cannot bind reference to sender by its interface only, but have to use mappedName
as you did.
Upvotes: 5
Reputation: 12538
AbstractClass
and not overridden in one of the EJBs, it seems to be practical to have one separate EJB implementing ServiceA
. Second, instead of having 2 different EJB implementations for the same interface getMetaValue
, I'd rather parameterize the interface and let the EJB decide whether it wants to implement new MetaValue(x, y)
or new MetaValue(a, b)
, e.g. abstract public MetaValue getMetaValue(SomeParameter param);
Upvotes: 2