Java Reference
In-Depth Information
In each case you use the @Audited interceptor binding. The @Audited interceptor bind-
ing is declared for either target ( @Target({TYPE, METHOD})) , so you use it for
both cases. You can declare interceptor bindings that may be used only at the class or
method level. Because @Audited is linked to AuditInterceptor , when the ad-
dBid() method is called, the invocation will be intercepted and the audit() method will
be called first.
This is a simple example showing how CDI interceptors work. CDI interceptors can be-
come much more powerful with interceptor bindings including other bindings, and inter-
ceptors declaring multiple bindings. We'll explore this next.
Beans.xml
Recall that when using CDI, your bean archive JAR file needs a META-INF/beans.xml file
for CDI to look in your JAR files for beans. In addition, when using CDI interceptors, all
the interceptors you want to activate must be listed in beans.xml. So the final step is to add
the AuditInterceptor to bean.xml:
<beans
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>com.bazaar.AuditInterceptor</class>
</interceptors>
</beans>
It may seem like an unnecessary step to have to list your interceptors in beans.xml, but the
AuditInterceptor example is simple with only one interceptor binding. What makes
CDI interceptors powerful are their ability to make new interceptor bindings on top of ex-
isting ones and to have both interceptors and beans use multiple bindings. Throw EJB in-
terceptors into the mix as well and the necessity of having interceptors listed in beans.xml
becomes clear. Next we'll look at multiple interceptor bindings.
Multiple bindings
When creating a new interceptor binding, existing bindings may be used to make the new
one more powerful. Suppose ActionBazaar has some EJB business logic that's secure. It's
Search WWH ::




Custom Search