Simple Enterprise JavaBeans 3.0 Examples 3-5
of the AccountBean. This shows how you can specify interceptor methods for both business methods and lifecycle callbacks in the bean file itself, in addition to
using an associated interceptor class.
3.3 Example of an Interceptor Class
The following code shows an example of an interceptor class, specifically the AuditInterceptor class that is referenced by the preceding AccountBean stateful
session bean with the Interceptors{examples.AuditInterceptor.class} annotation; the code
in bold is described after the example:
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 auditInvocationContext ic throws Exception { System.out.printlnInvoking method: +ic.getMethod;
return ic.proceed; }
PostActivate public void postActivateInvocationContext ic {
System.out.printlnInvoking method: +ic.getMethod; }
PrePassivate public void prePassivateInvocationContext ic {
System.out.printlnInvoking method: +ic.getMethod; }
}
The main points to notice about the preceding example are:
■
As usual, import the metadata annotations used in the file: import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext; import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
■
The interceptor class is plain Java class.
■
The class has an empty constructor: public AuditInterceptor {}
■
The method-level AroundInvoke specifies the business method interceptor method. You can use this annotation only once in an interceptor class.
■
The method-level PostActivate and PrePassivate annotations specify the methods that the EJB container should call after reactivating and before
passivating the bean, respectively.
3-6 Programming Enterprise JavaBeans, Version 3.0, for Oracle WebLogic Server
3.4 Example of Invoking a 3.0 Entity From A Session Bean
For an example of invoking an entity from a session bean, see the EJB 3.0 example in the distribution kit. After you have installed WebLogic Server, the example is in the
following directory:
WL_HOMEsamplesserverexamplessrcexamplesejbejb30 WL_HOME refers to the directory in which you installed WebLogic Server, such as
OracleMiddlewarewlserver_10.3.
Note: These lifecycle callback interceptor methods apply only to
stateful session beans.
4
Iterative Development of Enterprise JavaBeans 3.0 4-1
4
Iterative Development of Enterprise JavaBeans 3.0
The sections that follow describe the general EJB 3.0 implementation process, and provide guidance for how to get an EJB 3.0 up and in WebLogic Server.
For a review of WebLogic Server EJB features, see Section 2.2, WebLogic Server
Value-Added EJB 3.0 Features.
■
Section 4.1, Overview of the EJB 3.0 Development Process