Java Reference
In-Depth Information
The most common solution to these kind of problems is to add a little bit of code at
the beginning and end of every method. This approach has several problems: the
logic needs to be implemented several times, if we later wish to modify or remove
the functionality, we need to modify several methods.
Aspect-oriented programming is a paradigm that solves the above problems by
providing a way to implement the logic to be executed just before and/or just after a
method's main logic in a separate class. EJB 3.0 introduced the ability to implement
aspect oriented programming via interceptors .
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 5 JavaDoc (accessible within NetBeans by going to
Help | JavaDoc References | Java EE 5 SDK ) for more information.
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.
 
Search WWH ::




Custom Search