ヘッダーをスキップ
Oracle® Fusion Middleware Oracle WebLogic Server Enterprise JavaBeansバージョン3.0のプログラミング
11g リリース1 (10.3.6)
B61625-06
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

3 Enterprise JavaBeans 3.0の単純なサンプル

次の項では、新しいメタデータ・アノテーション・プログラミング・モデルを使用するEJBの単純なJavaサンプルについて説明します。

これらのサンプルを参照してEJBをプログラミングする方法については、このマニュアルの別の節で説明します。

単純なステートレス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!");
  }
}

このサンプル・コードで注目すべき主な点は次のとおりです。

単純なステートフル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つのメソッド(depositwithdraw、および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()");  
  }
}

このサンプル・コードで注目すべき主な点は次のとおりです。

インターセプタ・クラスのサンプル

次に、インターセプタ・クラスのサンプル・コードを示します。この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());
  }
}

このサンプル・コードで注目すべき主な点は次のとおりです。

セッションBeanから3.0エンティティを呼び出すサンプル

セッションBeanからエンティティを呼び出すサンプルについては、配布キットに含まれているEJB 3.0のサンプルを参照してください。WebLogic Serverをインストールすると、次のディレクトリにサンプルがインストールされます。

WL_HOME/samples/server/examples/src/examples/ejb/ejb30

WL_HOMEは、WebLogic Serverのインストール・ディレクトリ(たとえば/Oracle/Middleware/wlserver_10.3)です。