Java Reference
In-Depth Information
Second is the advice . The advice is the actual code you develop to handle whatever concern
you have. For example, if your concern is to log method entry events to a database, your
advice is the code that connects to the database and executes the query to insert the data.
Third is a pointcut . A pointcut describes a point in your application where you want to run
the code of your advice . Common pointcuts include before entry into a method and after
exiting from a method.
Fourth is an aspect . An aspect is a combination of a pointcut and advice. Your AOP imple-
mentation uses the aspect to weave in your advice and run it at the pointcuts.
An EJB 3 interceptor is the most general form of interception—it's an around-invoke ad-
vice. Interceptors are triggered at the entry into a method pointcut. The interceptor is still
around when the method returns, so the interceptor can inspect the method return value.
The interceptor can also catch any exceptions thrown by the method. Interceptors can be
applied to session and message-driven beans.
Although EJB 3 interceptors provide sufficient functionality to handle most common cross-
cutting concerns, they don't try to provide the level of functionality that a full-scale AOP
package such as AspectJ offers. On the flip side, EJB 3 interceptors are also generally a lot
easier to use. Now that you know the basics of interceptors, let's take a look at when to use
them.
5.3.3. When to use interceptors
EJB 3 interceptors are designed to be used with session beans and MDBs. EJB 3 intercept-
ors are an invoke-around aspect for methods, so you can use interceptors to perform some
kind of crosscutting concern logic before a method is executed, examine a return value
from an executed method, and catch any exceptions that a method may throw. When an
interceptor performs logic before a method is executed, it's usually for an auditing concern
(logging, statistic, and so on) or to verify and optionally change parameter values before
they're sent to the method. When an interceptor performs logic after the method returns
a value, it's again usually for an auditing concern or to alter the return value before actu-
ally returning. When an interceptor catches and handles exceptions thrown by the method,
again auditing is the most common concern, but the interceptor may also attempt to recall
Search WWH ::




Custom Search