EJB基礎筆記(一)

magic_dreamer發表於2010-07-02
EJB基礎筆記(一)

第一章 教程提示
第二章 EJB技術概述
2.1 簡介
2.2 兩層和三層環境
Two-tier Computing Environments: Application -----------network--------> Database Server
三層 客戶機-------》應用程式伺服器--------》資料庫伺服器

第三章 EJB規範
3.2 EJB容器
容器容納和管理 Enterprise Bean
Java Web 伺服器容納 Servlet
HTML瀏覽器容納 Java Applet

EJB 容器在執行時管理 Enterprise Bean 的各個方面,包括遠端訪問 bean、安全性、持續、事務、並行性和資源的訪問與合用。

EJB Container >Transaction Management
>Persistence Management
>Security Management
>EJBContext, JNDI ENC
>Callback Methods
由於客戶機應用程式不能直接訪問 bean -- 容器位於客戶機和 bean 之間。

Enterprise Bean 通過以下三種機制之一與容器互動:
回撥方法
每個 bean 都會實現 EnterpriseBean 介面的子型別,該介面定義了一些方法,稱作回撥方法。每個回撥方法在 bean 的生命週期期間向它提示一個不同事件,當容器要合用某個 bean、將其狀態儲存到資料庫、結束事務、從記憶體中除去該 bean 等操作時,它將呼叫這些方法來通知該 bean。回撥方法可以讓 bean 在事件之前或之後立即執行內部調整。

EJBContext介面
每個 bean 都會得到一個 EJBContext 物件,它是對容器的直接引用。EJBContext 介面提供了用於與容器互動的方法,因此那個 bean 可以請求關於環境的資訊,如其客戶機的身份或事務的狀態,或者 bean 可以獲取它自身的遠端引用。

Java 命名和目錄介面 (JNDI)
JNDI 是 Java 平臺的標準擴充套件,用於訪問命名系統,如 LDAP、NetWare、檔案系統等。每個 bean 自動擁有對某個特定命名系統(稱作環境命名上下文 (ENC))的訪問權。ENC 由容器管理,bean 使用 JNDI 來訪問 ENC。JNDI ENC 允許 bean 訪問資源,如 JDBC 連線、其它 Enterprise Bean,以及特定於該 bean 的屬性。

3.5 遠端和本地介面
遠端和本地介面分別擴充套件 javax.ejb.EJBObject 和 javax.ejb.EJBHome 介面。這些 EJB 介面型別定義了一組標準的實用程式方法,併為所有遠端和本地介面提供了常用基本型別。

Remote Interface:java.rmi.Remote ----->javax.ejb.EJBObject ------->Customer
Home Interface: java.rmi.Remote ----->javax.ebj.EJBHome -------->CustomerHome

CustomerHome home = // ... obtain a reference that implements the home interface.

// Use the home interface to create a
// new instance of the Customer bean.
Customer customer = home.create(customerID);

// using a business method on the Customer.
customer.setName(someName);

The customer interface:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Customer extends EJBObject {
public Name getName() throws RemoteException;
public void setName(Name name) throws RemoteException;
public Address getAddress() throws RemoteException;
public void setAddress(Address address) throws RemoteException;

}

實體 Bean,它表示持久商業物件(資料儲存在資料庫中的商業物件)。實體 Bean 表示資料庫中的商業資料,並新增特定於該資料的行為。

會話 Bean
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface HotelClerk extends EJBObject {
public void reserveRoom(Customer cust, RoomInfo ri, Date from, Date to)
throws RemoteException;
public RoomInfo availableRooms(Location loc, Date from, Date to)
throws RemoteException;
}
HotelClerk bean 充當代理程式,因為它代表使用者執行任務,但它自己在資料庫中並不是持久的。您不需要有關 HotelClerk 的資訊;您需要旅館店員為您執行任務。這是會話 Bean 的典型行為。

實體Bean
import javax.ejb.EntityBean;

public class CustomerBean implements EntityBean {
Address myAddress;
Name myName;
CreditCard myCreditCard;

snip getter and setter
...
}

Bean Class : javax.ejb.EnterpriseBean -- > javax.ejb.EntityBean -- > CustomerBean

3.9 生命週期方法
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.rmi.RemoteException;

public interface CustomerHome extends EJBHome {
public Customer create(Integer customerNumber) throws RemoteException, CreateException;
public Customer findByPrimaryKey(Integer customerNumber) throws RemoteException, FinderException;
public Enumeration findByZipCode(int zipCode) throws RemoteException, FinderException;
}
create() 方法用於建立新的實體。這將在資料庫中產生新的記錄。本地介面可能有許多 create() 方法。每個 create() 的自變數數量和資料型別由 bean 開發人員指定,但返回型別必須是遠端介面資料型別。這種情況下,在 CustomerHome 介面上呼叫 create() 將返回 Customer 的例項。

第四章 實體Bean
實體Bean: 容器管理的持續(CMP) bean管理的持續(BMP)
容器管理的持續
容器管理的欄位可以是任何原語資料型別或可序列化型別。本例中使用原語 int (customerID) 和可序列化物件(Address、Name、CreditCard)。

4.4 本地介面
public interface CustomerHome extends javax.ejb.EJBHome {
public Customer create(Integer customerNumber) throws RemoteException,CreateException;
public Customer create(Integer customerNumber, Name name) throws RemoteException,CreateException;
public Customer findByPrimaryKey(Integer customerNumber) throws RemoteException, FinderException;
public Enumeration findByZipCode(int zipCode) throws RemoteException, FinderException;
}
呼叫本地介面示例:
CustomerHome home = // Get a reference to the CustomerHome object
Name name = new Name("John", "W", "Smith");
Customer customer = home.create(new Integer(33), name);

4.5 遠端介面
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Customer extends EJBObject {
public Name getName() throws RemoteException;
public void setName(Name name) throws RemoteException;
public Address getAddress() throws RemoteException;
public void setAddress(Address address) throws RemoteException;
public CreditCard getCreditCard() throws RemoteException;
public void setCreditCard(CreditCard card) throws RemoteException;
}

遠端介面呼叫示例:
Customer customer = // ... obtain a remote reference to the bean
// get the customer's address
Address addr = customer.getAddress();
// change the zip code
addr.zip = "56777";
// update the customer's address
customer.setAddress(addr);

4.6 回撥方法
回撥方法在 javax.ejb.EntityBean 介面中定義,此介面由所有實體 Bean 實現,包括 CustomerBean 類。
public interface javax.ejb.EntityBean {
public void setEntityContext();
public void unsetEntityContext();
public void ejbLoad();
public void ejbStore();
public void ejbActivate();
public void ejbPassivate();
public void ejbRemove();
}
使用回撥方法的CustomerBean:
import javax.ejb.EntityBean;
public class CustomerBean implements EntityBean {
int customerID;
Address myAddress;
Name myName;
CreditCard myCreditCard;
EntityContext ejbContext;
// CALLBACK METHODS
public void setEntityContext(EntityContext cntx) {
ejbContext = cntx
}
// BUSINESS METHODS
public void setCreditCard(CreditCard card) {
if (card.type.equals("WorldWide"))
if (ejbContext.isCallerInRole("Manager"))
myCreditCard = card;
else
throw new SecurityException();
else
myCreditCard = card;
}
...snip...

4.8 Bean管理的持續
bean 使用資料庫 API(通常是 JDBC)來讀取其欄位並將欄位寫入資料庫,但容器會告訴它何時執行每個同步操作,並會自動管理 bean 的事務。bean 管理的持續可以讓 bean 開發人員靈活地執行對於容器來說太過於複雜的持續操作,或者使用容器不支援的資料來源 -- 例如,定製或舊的資料庫。
public class CustomerBean_BMP extends CustomerBean {
public void ejbLoad() {
// override implementation
}
public void ejbStore() {
// override implementation
}
public void ejbCreate() {
// override implementation
}
public void ejbRemove() {
// override implementation
}
private Connection getConnection() {
// new helper method
}
}

ejbLoad() 和 ejbStore() 方法必須包括資料庫訪問邏輯,只有這樣,當 EJB 告訴 bean 要裝入和儲存資料時,它才能這麼做。容器會在適當時自動執行 ejbLoad() 和 ejbStore() 方法。

ejbCreate() 方法將新記錄插入資料庫,而 ejbRemove() 方法從資料庫中刪除實體的資料。

獲得ID Integer primaryKey = (Integer)ejbContext.getPrimaryKey();

相關文章