Java Reference
In-Depth Information
An interceptor binding also has the
java.lang.annotation.Inherited
annota-
tion, 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
@In-
Every
@AroundInvoke
method takes a
javax.interceptor.InvocationContext
argument, returns a
java.lang.Object
, and throws an
Exception
. It can call
InvocationCon-
text
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
ex-
ample, the
PaymentHandler
bean is annotated
@Logged
, which means that any in-
vocation of its business methods will cause the interceptor's
@AroundInvoke
method
to be invoked:
@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:
@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: