74 Chapter 3 There (Web site translator) are two good reasons

74 Chapter 3 There are two good reasons not to implement your bean s component interface: Reason 1. Component interfaces extend interfaces defined by Sun, such as javax.ejb.EJBObject or javax.ejb.EJBLocalObject. These superinterfaces define additional methods intended for client use, and you d therefore have to provide no-op implementations of those methods in your bean. Those methods have no place in your bean class. Reason 2. Let s assume your enterprise bean wants to call a method on a different enterprise bean, and you want to pass a reference to your bean as a parameter to the other bean s method (similar to passing the this parameter in Java). How can you do this in EJB? Remember that all clients call methods on EJB objects, not beans. Thus, if your bean calls another bean, you must pass a reference to your bean s EJB object, rather than a reference to your bean. The other bean should operate on your EJB object, and not your bean, because the other bean is a client, just like any other client, and all clients must go through EJB objects. The danger here is if your enterprise bean class implements your EJB object s remote interface. You could accidentally pass a reference to the bean itself, rather than pass a reference to the bean s EJB object. Because your bean implements the same interface as the EJB object, the compiler would let you pass the bean itself as a this parameter, which is an error. A Solution There is an alternative way to preserve compile-time checks of your method signatures. The approach is to contain your bean s business method signatures within a common superinterface that your remote interface extends and your bean implements. You can think of this superinterface as a business interface that defines your business methods and is independent of EJB. The following example illustrates this concept: // Business interface public interface HelloBusinessMethods { public String hello() throws java.rmi.RemoteException; } // EJB remote interface public interface HelloRemote extends javax.ejb.EJBObject, HelloBusinessMethods { } // EJB local interface public interface HelloLocal extends javax.ejb.EJBLocalObject, HelloBusinessMethods { }
We offer quality web hosting with only $3.99 per month with unlimited email addresses, unlimited bandwidth, and unlimited server space. Check our web hosting unlimited bandwidth section.

Leave a Reply