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
)です。