系統闡述 EJB 開發的四大階段:開發、配置、組裝、管理,深度解析 Bean 類、Remote/Home 介面、主鍵類、繼承體系、方法規約、生命週期差異等規範細節。
SessionBean 或 EntityBean 介面。EJBObject,聲明客戶端可調用的業務方法,拋 RemoteException。EJBHome,聲明 create()、find()、remove() 生命週期方法。ejb-jar.xml(XML 部署描述器),聲明 Bean 屬性、環境條目、資源引用、安全角色、事務屬性。.jar。env-entry),由容器注入。create() ↔ Bean ejbCreate():參數一致,返回類型不同(Remote 介面 vs 主鍵/void)。Remote → EJBObject → Remote
Home → EJBHome → Remote
SessionBean → EnterpriseBean → Serializable
EntityBean → EnterpriseBean → Serializable
getEJBHome()、getPrimaryKey()、remove()、getHandle()、isIdentical()
remove(Handle)、remove(PrimaryKey)、getEJBMetaData()、getHomeHandle()
create() 方法,返回 Remote 介面。ejbCreate() 對應一個 create()。RemoteException、CreateException。find(),因有狀態 Session Bean 專屬於創建者。create() + 強制 findByPrimaryKey(PrimaryKey)(返回單一 Remote)。findXxx() 可返回 Collection/Enumeration。ejbFindXxx()。RemoteException、FinderException。提供 getHomeInterfaceClass()、getRemoteInterfaceClass()、getPrimaryKeyClass()、isSession()、isStatelessSession(),支援構建時類型檢查與部署驗證。
⚠️ 現代架構已轉向 EJB 3.x / Spring / CDI:無介面、注解驅動、POJO 開發、依賴注入替代 JNDI 查找。本文適用於維護 EJB 2.x 遺產系統或理解容器底層契約。