Java Reference
In-Depth Information
Implementing aspect oriented programming via interceptors consists of two
steps: coding the interceptor class and decorating the EJBs to be intercepted
with the
@Interceptors
annotation.
Implementing the interceptor class
An interceptor is a standard Java class, it must have a single method with the
following signature:
@AroundInvoke
public Object methodName(InvocationContext invocationContext) throws
Exception
Notice that the method must be decorated with the
@AroundInvoke
annotation,
which marks the method as an interceptor method. The
InvocationContext
parameter can be used to obtain information from the intercepted method, such as
its name, parameters, the class that declares it, and more. It also has a
proceed()
method that is used to indicate when to execute the method logic.
The following table summarizes some of the most useful
InvocationContext
methods. Refer to the Java EE 6 JavaDoc (accessible within NetBeans by going to
Help | JavaDoc References | Java EE 6 - DRAFT
).
Method name
Description
getMethod()
Returns an instance of
java.lang.reflect.Method
that can
be used to introspect the intercepted method.
getParameters()
Returns an array of Objects containing the parameters passed to
the intercepted method.
getTarget()
Returns the object containing the method being invoked,
return value is
java.lang.Object
.
proceed()
Invokes the method being intercepted.
The following example illustrates a simple interceptor class.
package com.ensode.sessionbeanintro.ejb;
import java.lang.reflect.Method;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class LoggingInterceptor {
@AroundInvoke
public Object logMethodCall(
InvocationContext invocationContext)
throws Exception {