type=page
status=published
title=Using Interceptors in CDI Applications
next=cdi-adv007.html
prev=cdi-adv005.html
~~~~~~
= Using Interceptors in CDI Applications


[[GKHJX]]

[[using-interceptors-in-cdi-applications]]
Using Interceptors in CDI Applications
--------------------------------------

An interceptor is a class used to interpose in method invocations or
lifecycle events that occur in an associated target class. The
interceptor performs tasks, such as logging or auditing, that are
separate from the business logic of the application and are repeated
often within an application. Such tasks are often called cross-cutting
tasks. Interceptors allow you to specify the code for these tasks in one
place for easy maintenance. When interceptors were first introduced to
the Java EE platform, they were specific to enterprise beans. On the
Java EE platform, you can use them with Java EE managed objects of all
kinds, including managed beans.

For information on Java EE interceptors, see
link:interceptors.html#GKEED[Chapter 57, "Using Java EE Interceptors"].

An interceptor class often contains a method annotated `@AroundInvoke`,
which specifies the tasks the interceptor will perform when intercepted
methods are invoked. It can also contain a method annotated
`@PostConstruct`, `@PreDestroy`, `@PrePassivate`, or `@PostActivate`, to
specify lifecycle callback interceptors, and a method annotated
`@AroundTimeout`, to specify EJB timeout interceptors. An interceptor
class can contain more than one interceptor method, but it must have no
more than one method of each type.

Along with an interceptor, an application defines one or more
interceptor binding types, which are annotations that associate an
interceptor with target beans or methods. For example, the `billpayment`
example contains an interceptor binding type named `@Logged` and an
interceptor named `LoggedInterceptor`. The interceptor binding type
declaration looks something like a qualifier declaration, but it is
annotated with `javax.interceptor.InterceptorBinding`:

[source,oac_no_warn]
----
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}
----

An interceptor binding also has the `java.lang.annotation.Inherited`
annotation, to specify that the annotation can be inherited from
superclasses. The `@Inherited` annotation also applies to custom scopes
(not discussed in this tutorial) but does not apply to qualifiers.

An interceptor binding type may declare other interceptor bindings.

The interceptor class is annotated with the interceptor binding as well
as with the `@Interceptor` annotation. For an example, see
link:cdi-adv-examples005.html#GKHRQ[The LoggedInterceptor Interceptor
Class].

Every `@AroundInvoke` method takes a
`javax.interceptor.InvocationContext` argument, returns a
`java.lang.Object`, and throws an `Exception`. It can call
`InvocationContext` methods. The `@AroundInvoke` method must call the
`proceed` method, which causes the target class method to be invoked.

Once an interceptor and binding type are defined, you can annotate beans
and individual methods with the binding type to specify that the
interceptor is to be invoked either on all methods of the bean or on
specific methods. For example, in the `billpayment` example, the
`PaymentHandler` bean is annotated `@Logged`, which means that any
invocation of its business methods will cause the interceptor's
`@AroundInvoke` method to be invoked:

[source,oac_no_warn]
----
@Logged
@SessionScoped
public class PaymentHandler implements Serializable {...}
----

However, in the `PaymentBean` bean, only the `pay` and `reset` methods
have the `@Logged` annotation, so the interceptor is invoked only when
these methods are invoked:

[source,oac_no_warn]
----
@Logged
public String pay() {...}

@Logged
public void reset() {...}
----

In order for an interceptor to be invoked in a CDI application, it must,
like an alternative, be specified in the `beans.xml` file. For example,
the `LoggedInterceptor` class is specified as follows:

[source,oac_no_warn]
----
<interceptors>
    <class>javaeetutorial.billpayment.interceptors.LoggedInterceptor</class>
</interceptors>
----

If an application uses more than one interceptor, the interceptors are
invoked in the order specified in the `beans.xml` file.

The interceptors that you specify in the `beans.xml` file apply only to
classes in the same archive. Use the `@Priority` annotation to specify
interceptors globally for an application that consists of multiple
modules, as in the following example:

[source,oac_no_warn]
----
@Logged
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LoggedInterceptor implements Serializable { ... }
----

Interceptors with lower priority values are called first. You do not
need to specify the interceptor in the `beans.xml` file when you use the
`@Priority` annotation.
