Java Reference
In-Depth Information
Interceptors
The Interceptors 1.2 Specification was developed under JSR 318. This section just
gives you an overview of improvements in the API. The complete document specific-
ation (for more information) can be downloaded from http://jcp.org/aboutJava/com-
munityprocess/final/jsr318/index.html .
Intercepting some processes
Interceptors are a Java mechanism that allows us to implement some concepts of
AOP, in the sense that they give us the ability to separate the code from the cross-
cutting concerns such as logging, auditing, and security. Thus, due to this specifica-
tion, we can intercept invocations of methods, lifecycle callback events, and timeout
events.
Interceptors allow you to intercept method calls as well as the outbreak of some
events. During the interception, you can access the method name, method paramet-
ers, and a lot of other information. That said, the interceptors can be used to manage
cross cutting concerns such as logging, auditing, security (to ensure that a user has
the right to execute a method), and modification of the method parameters. You can
define them in a dedicated class or within the target class directly.
The signature of an interceptor is as follows: Object <meth-
od_name>(InvocationContext ctx) throws Exception { ... }" and
to void <method_name>(InvocationContext ctx) { ... } . It can throw
an exception of type Exception and should be decorated with an annotation that
defines the type of elements it must intercept. For example, @AroundInvoke to in-
tercept methods and @AroundTimeout to intercept services' timers. Failing to use
these annotations, you can always make use of XML configuration.
Defining interceptors in the target class
The following code shows a session bean with method and timer service interceptors.
The service timer interceptor ( targetClassTimerInterceptor ) only does the
logging, while the method interceptor ( targetClassMethodInterceptor ), in ad-
dition to a little logging, demonstrates how to access and modify the parameters of
Search WWH ::




Custom Search