Overview of Interceptors
Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.
Although interceptors are part of Enterprise JavaBeans 3.2 and Contexts
and Dependency Injection for Java EE 1.1, the Interceptors 1.2
specification is downloadable as part of a maintenance release of JSR
318, Enterprise JavaBeans 3.1, available from
An interceptor can be defined within a target class as an interceptor method, or in an associated class called an interceptor class. Interceptor classes contain methods that are invoked in conjunction with the methods or lifecycle events of the target class.
Interceptor classes and methods are defined using metadata annotations, or in the deployment descriptor of the application that contains the interceptors and target classes.
Interceptor methods within the target class or in an interceptor class are annotated with one of the metadata annotations defined in Table 57-1.
Table 57-1 Interceptor Metadata Annotations
Interceptor classes may be designated with the optional
The target class can have any number of interceptor classes associated
with it. The order in which the interceptor classes are invoked is
determined by the order in which the interceptor classes are defined in
Interceptor classes may be targets of dependency injection. Dependency
injection occurs when the interceptor class instance is created, using
the naming context of the associated target class, and before any
Interceptor classes have the same lifecycle as their associated target
class. When a target class instance is created, an interceptor class
instance is also created for each declared interceptor class in the
target class. That is, if the target class declares multiple interceptor
classes, an instance of each class is created when the target class
instance is created. The target class instance and all interceptor class
instances are fully instantiated before any
Interceptors and CDI
Contexts and Dependency Injection for Java EE (CDI) builds on the basic functionality of Java EE interceptors. For information on CDI interceptors, including a discussion of interceptor binding types, see Using Interceptors in CDI Applications.