EJB component interfaces (Remote/Local) extend EJB-specific interfaces (EJBObject, EJBLocalObject) which have methods meant for clients, not beans. If beans don’t implement the component interface (to avoid pollution), how do we get compile-time checking that the bean has all required business methods?
Define a “pure” business interface containing only your business method signatures—no EJB dependencies. Have both the Remote/Local interface AND the bean class implement this business interface. This gives compile-time checking without polluting the bean with EJB client methods.
// 1. Pure business interface (no EJB stuff)
public interface HelloBusinessMethods {
String hello() throws RemoteException;
}
// 2. Remote interface extends BOTH EJBObject and business interface
public interface HelloRemote extends javax.ejb.EJBObject, HelloBusinessMethods {}
// 3. Local interface also extends business interface
public interface HelloLocal extends javax.ejb.EJBLocalObject, HelloBusinessMethods {}
// 4. Bean implements business interface (+ SessionBean)
public class HelloBean implements SessionBean, HelloBusinessMethods {
public String hello() { return "Hello, World!"; }
}- Compile-time safety: If bean misses a business method, compilation fails
- No pollution: Bean doesn’t implement
EJBObjectmethods - Shared contract: Both bean and EJB object share the same business method signatures
- One downside: Local interface inherits
RemoteExceptionfrom business interface (if business interface declares it)
graph semantic__Business_Interface_Pattern_ {
layout=neato
node [shape=ellipse fontname="Helvetica" fontsize=11 style=filled]
THIS [label=""Business Interface " fillcolor="#ffd700" fontsize=13 style="filled,bold"]
REL1 [label="Related Concept" fillcolor="#f0f0f0"]
REL2 [label="Builds Into" fillcolor="#d4edda"]
THIS -- REL1 [label="related"]
THIS -- REL2 [label="builds into"]
}- Built from: Why Bean Doesn't Implement Component Interface
- Builds into: Remote Interface, Local Home Interface
- Related: Session Bean, Entity Bean
- Contrasts with: Direct implementation (bean implements remote interface directly)
RemoteExceptionleakage: Business interface meant for both remote and local still declaresRemoteException—local clients don’t need it- EJB 3.x+ solves this: Uses
@Localand@Remoteannotations—no need for this pattern - Not mandatory: Most developers just let the container verify at deployment time (not compile time)