Java Reference
In-Depth Information
Everything we've covered so far has been about EJB interceptors. Though extremely useful
and powerful, EJB interceptors are a simple implementation meant to be used only with
Enterprise beans. Next, we'll look at how EJB interceptors compare to the more powerful
CDI interceptors.
5.3.8. CDI versus EJB interceptors
EJB interceptors are very powerful, but they provide only a basic framework for imple-
menting interceptors. CDI takes this a step further by providing type-safe interceptor bind-
ings that can be combined in a number of different ways to provide much more advanced
options when implementing interceptors. We'll look at how to use these interceptor bind-
ings, and you'll immediately be able to see the advantages over plain EJB interceptors.
Creating interceptor bindings
Let's suppose ActionBazaar has some kind of auditing concern and you want to use a CDI
interceptor to handle this. The first step is to create an interceptor binding. An interceptor
binding is a type-safe link between an interceptor and your EJB. Its name should indicate
what the interceptor binding does. In the example you have an auditing concern, so you'll
create an interceptor binding named @Audited :
@Target({TYPE, METHOD})
public @interface Audited {}
As you can see, an interceptor binding is a custom annotation declared with @javax
.interceptor.InterceptorBinding . The @Audited interceptor binding may
now be used in your code as a go-between to link interceptors with beans. To perform this
link, the interceptor binding must be applied to both the interceptor and the bean. Let's first
look at how to apply the interceptor binding to the interceptor.
Declaring bindings for an interceptor
When creating a CDI interceptor class (the advice in the AOP world), you must declare that
the class is an interceptor and declare what interceptor bindings are on the class. Continu-
ing with the auditing example, create the AuditInterceptor :
Search WWH ::

Custom Search