Java Reference
In-Depth Information
Interceptors and CDI
Contexts and Dependency Injection for the Java EE Platform (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
”
on
page
260
.
Using Interceptors
An interceptor is defined using one of the interceptor metadata annotations listed in
Table
23-1
within the target class, or in a separate interceptor class. The following code declares
an
@AroundTimeout
interceptor method within a target class.
@Stateless
public class TimerBean {
...
@Schedule(minute="*/1", hour="*")
public void automaticTimerMethod() { ... }
@AroundTimeout
public void timeoutInterceptorMethod(InvocationContext ctx) {
... }
...
}
If interceptor classes are used, use the
javax.interceptor.Interceptors
an-
notation to declare one or more interceptors at the class or method level of the target class.
The following code declares interceptors at the class level.
@Stateless
@Interceptors({PrimaryInterceptor.class, SecondaryIntercept-
or.class})
public class OrderBean { ... }
The following code declares a method-level interceptor class.
@Stateless
public class OrderBean {
...
@Interceptors(OrderInterceptor.class)
public void placeOrder(Order order) { ... }