| Oracle® Fusion Middleware Oracle WebLogic Server Enterprise JavaBeansバージョン3.0のプログラミング 11g リリース1 (10.3.5) B61625-03 |
|
![]() 前 |
![]() 次 |
以下の節では、新しいメタデータ・アノテーション・プログラミング・モデルを使用するEJBの単純なJavaサンプルについて説明します。
これらのサンプルを参照してEJBをプログラミングする方法については、このマニュアルの別の節で説明します。
次に、ServiceBeanステートレス・セッションEJBの単純なビジネス・インタフェースのコードを示します。
package examples;
/**
* Business interface of the Service stateless session EJB
*/
public interface Service {
public void sayHelloFromServiceBean();
}
このコードは、Serviceビジネス・インタフェースに、1つのメソッドsayHelloFromServiceBean()が含まれていることを示します。このメソッドは、パラメータを取らず、voidを返します。
次に、上のServiceインタフェースを実装するBeanファイルのコードを示します。太字部分については、後ほど解説します。
package examples; import static javax.ejb.TransactionAttributeType.*; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.interceptor.ExcludeDefaultInterceptors; /** * Bean file that implements the Service business interface. * Class uses following EJB 3.0 annotations: * - @Stateless - specifies that the EJB is of type stateless session * - @TransactionAttribute - specifies that the EJB never runs in a * transaction * - @ExcludeDefaultInterceptors - specifies any configured default * interceptors should not be invoked for this class */ @Stateless @TransactionAttribute(NEVER) @ExcludeDefaultInterceptors public class ServiceBean implements Service { public void sayHelloFromServiceBean() { System.out.println("Hello From Service Bean!"); } }
このサンプル・コードで注目すべき主な点は以下のとおりです。
標準のimport文を使用して、Beanファイルで使用するメタデータ・アノテーションをインポートしています。
import static javax.ejb.TransactionAttributeType.*; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.interceptor.ExcludeDefaultInterceptors
EJB 3.0にのみ適用されるアノテーションは、javax.ejbパッケージに含まれています。他のJava Platform, Enterprise Edition (Java EE)バージョン5コンポーネントで使用できるアノテーションは、より一般的なパッケージ(javax.interceptor、javax.annotationなど)に含まれています。
ServiceBean Beanファイルは、Serviceビジネス・インタフェースを実装するプレーンJavaファイルで、EJB固有のインタフェースを実装する必要はありません。つまり、Beanファイルには、2.Xプログラミング・モデルでは必須だったライフサイクル・メソッド(ejbCreate、ejbPassivateなど)を実装する必要はないということです。
クラス・レベルの@Statelessメタデータ・アノテーションは、EJBのタイプがステートレス・セッションであることを指定しています。
クラス・レベルの@TransactionAttribute(NEVER)アノテーションは、EJBがトランザクション内で実行されることはないことを指定しています。
クラス・レベルの@ExcludeDefaultInterceptorsアノテーションは、ejb-jar.xmlデプロイメント記述子ファイルでデフォルト・インターセプタが定義されている場合でも、このEJBのメソッド呼出しに対しては、そのデフォルト・インターセプタを呼び出さないことを指定しています。
次に、AccountBeanステートフル・セッションEJBの単純なビジネス・インタフェースのコードを示します。
package examples;
/**
* Business interface for the Account stateful session EJB.
*/
public interface Account {
public void deposit(int amount);
public void withdraw(int amount);
public void sayHelloFromAccountBean();
}
このコードは、Accountビジネス・インタフェースに3つのメソッド(deposit、withdraw、およびsayHelloFromAccountBean)が含まれていることを示します。
次に、上のAccountインタフェースを実装するBeanファイルのコードを示します。太字部分については、後ほど解説します。
package examples; import static javax.ejb.TransactionAttributeType.*; import javax.ejb.Stateful; import javax.ejb.TransactionAttribute; import javax.ejb.Remote; import javax.ejb.EJB; import javax.annotation.PreDestroy; import javax.interceptor.Interceptors; import javax.interceptor.ExcludeClassInterceptors; import javax.interceptor.InvocationContext; /** * Bean file that implements the Account business interface. * Uses the following EJB annotations: * - @Stateful: specifies that this is a stateful session EJB * - @TransactionAttribute - specifies that this EJB never runs * in a transaction * - @Remote - specifies the Remote interface for this EJB * - @EJB - specifies a dependency on the ServiceBean stateless * session ejb * - @Interceptors - Specifies that the bean file is associated with an * Interceptor class; by default all business methods invoke the * method in the interceptor class annotated with @AroundInvoke. * - @ExcludeClassInterceptors - Specifies that the interceptor methods * defined for the bean class should NOT fire for the annotated * method. * - @PreDestroy - Specifies lifecycle method that is invoked when the * bean is about to be destoryed by EJB container. * */ @Stateful @TransactionAttribute(NEVER) @Remote({examples.Account.class}) @Interceptors({examples.AuditInterceptor.class}) public class AccountBean implements Account { private int balance = 0; @EJB(beanName="ServiceBean") private Service service; public void deposit(int amount) { balance += amount; System.out.println("deposited: "+amount+" balance: "+balance); } public void withdraw(int amount) { balance -= amount; System.out.println("withdrew: "+amount+" balance: "+balance); } @ExcludeClassInterceptors public void sayHelloFromAccountBean() { service.sayHelloFromServiceBean(); } @PreDestroy public void preDestroy() { System.out.println("Invoking method: preDestroy()"); } }
このサンプル・コードで注目すべき主な点は以下のとおりです。
標準のimport文を使用して、Beanファイルで使用するメタデータ・アノテーションをインポートしています。
import static javax.ejb.TransactionAttributeType.*; import javax.ejb.Stateful; import javax.ejb.TransactionAttribute; import javax.ejb.Remote; import javax.ejb.EJB; import javax.annotation.PreDestroy; import javax.interceptor.Interceptors; import javax.interceptor.ExcludeClassInterceptors;
EJB 3.0にのみ適用されるアノテーションは、javax.ejbパッケージに含まれています。他のJava Platform, Enterprise Edition (Java EE)バージョン5コンポーネントで使用できるアノテーションは、より一般的なパッケージ(javax.interceptor、javax.annotationなど)に含まれています。
インターセプタ間の状態を保持するために使用するInvocationContextクラスをインポートしています。
import javax.interceptor.InvocationContext;
AccountBean Beanファイルは、Accountビジネス・インタフェースを実装するプレーンJavaファイルで、EJB固有のインタフェースを実装する必要はありません。つまり、Beanファイルには、2.Xプログラミング・モデルでは必須だったライフサイクル・メソッド(ejbCreate、ejbPassivateなど)を実装する必要はないということです。
クラス・レベルの@Statefulメタデータ・アノテーションは、EJBのタイプがステートフル・セッションであることを指定しています。
クラス・レベルの@TransactionAttribute(NEVER)アノテーションは、EJBがトランザクション内で実行されることはないことを指定しています。
クラス・レベルの@Remoteアノテーションは、EJBのリモート・インタフェースの名前(ここでは、ビジネス・インタフェースと同じAccount)を指定しています。
クラス・レベルの@Interceptors({examples.AuditInterceptor.class})アノテーションは、Beanファイルに関連付けるインターセプタ・クラスを指定しています。通常、このクラスには、ビジネス・メソッド・インターセプタ・メソッドとライフサイクル・コールバック・インターセプタ・メソッドが含まれます。このクラスの詳細は、「インターセプタ・クラスのサンプル」を参照してください。
フィールド・レベルの@EJBアノテーションは、アノテーション変数serviceに、依存先であるServiceBeanステートレス・セッションBeanのコンテキストを注入することを指定しています。注入されるフィールドのデータ型はServiceです。これは、ServiceBean EJBのビジネス・インタフェースです。sayHelloFromAccountBeanメソッド内の次のコードは、依存先であるServiceBeanのsayHelloFromServiceBeanメソッドを呼び出す方法を示しています。
service.sayHelloFromServiceBean();
メソッド・レベルの@ExcludeClassInterceptorsアノテーションは、sayHelloFromAccountBeanメソッドに対しては、関連付けられているインターセプタ・クラス(AuditInterceptor)で指定されている@AroundInvokeメソッドを呼び出さないことを指定しています。
メソッド・レベルの@PreDestroyアノテーションは、EJBコンテナがAccountBeanのインスタンスを破棄する前にpreDestroyメソッドを呼び出すことを指定しています。ビジネス・メソッドおよびライフサイクル・コールバックのインターセプタ・メソッドは、インターセプタ・クラスを関連付ける方法だけでなく、このようにBeanファイル自体の中で指定することもできます。
次に、インターセプタ・クラスのサンプル・コードを示します。このAuditInterceptorクラスは、上のサンプルで示したAccountBeanステートフル・セッションBeanから@Interceptors({examples.AuditInterceptor.class})アノテーションで参照しているインターセプタ・クラスです。太字部分については、後ほど解説します。
package examples; import javax.interceptor.AroundInvoke; import javax.interceptor.InvocationContext; import javax.ejb.PostActivate; import javax.ejb.PrePassivate; /** * Interceptor class. The interceptor method is annotated with the * @AroundInvoke annotation. */ public class AuditInterceptor { public AuditInterceptor() {} @AroundInvoke public Object audit(InvocationContext ic) throws Exception { System.out.println("Invoking method: "+ic.getMethod()); return ic.proceed(); } @PostActivate public void postActivate(InvocationContext ic) { System.out.println("Invoking method: "+ic.getMethod()); } @PrePassivate public void prePassivate(InvocationContext ic) { System.out.println("Invoking method: "+ic.getMethod()); } }
このサンプル・コードで注目すべき主な点は以下のとおりです。
これまでのサンプルと同様に、ファイル内で使用するメタデータ・アノテーションをインポートしています。
import javax.interceptor.AroundInvoke; import javax.interceptor.InvocationContext; import javax.ejb.PostActivate; import javax.ejb.PrePassivate;
インターセプタ・クラスはプレーンJavaクラス。
このクラスには、次のような空のコンストラクタが含まれています。
public AuditInterceptor() {}
メソッド・レベルの@AroundInvokeは、ビジネス・メソッド・インターセプタ・メソッドを指定しています。このアノテーションは、1つのインターセプタ・クラス内で一度だけ使用できます。
メソッド・レベルの@PostActivateおよび@PrePassivateアノテーションは、EJBコンテナがBeanを再アクティブ化した後とBeanをパッシブ化する前に呼び出すメソッドをそれぞれ指定しています。
|
注意: これらのライフサイクル・コールバック・インターセプタ・メソッドは、ステートフル・セッションBeanにのみ適用されます。 |
セッションBeanからエンティティを呼び出すサンプルについては、配布キットに含まれているEJB 3.0のサンプルを参照してください。WebLogic Serverをインストールすると、次のディレクトリにサンプルがインストールされます。
WL_HOME/samples/server/examples/src/examples/ejb/ejb30
WL_HOMEは、WebLogic Serverのインストール・ディレクトリ(たとえば/Oracle/Middleware/wlserver_10.3)です。