12.4.1. Interceptor bindings
Creating an interceptor with CDI involves four different coding tasks. Although these tasks
aren't nearly as elegant as defining a point cut with AspectJ, they do provide you with a
strong degree of predictability at runtime. The tasks are as follows:
1 . Create an interceptor annotation.
2 . Create an interceptor implementation annotated with the interceptor annotation.
3 . Annotate targeted instances with the interceptor annotation.
4 . Enable the interceptor with the beans.xml file.
There are three different kinds of interceptors, depending on the type of method and wheth-
we'll focus on a business method interceptor, but the other two are straightforward and no
Table 12.2. Interceptor types
Intercepts business method invocations
Intercepts lifecycle callbacks
Intercepts EJB timeout methods
To define an interceptor, you need to first define an interceptor annotation. The interceptor
annotation is used to associate the class or method to be intercepted with the interceptor
implementation. Thus, the annotation is placed on both the interceptor implementation and
the class or method you want intercepted. The interceptor isn't enabled by default; you
must enable it via the beans.xml file. To get a better grasp of this, let's look at an example
As with any important application, performance is an important consideration. During QA
testing, you'll want to collect performance information on key interfaces so that you can
establish a benchmark for the application. Thus, as in the last section, you'll use a separate
beans.xml file for QA versus actual application production. The performance interceptor
will track the execution time and write the statistics out to a flat file on the hard drive of