Skip to content

Latest commit

 

History

History
76 lines (63 loc) · 2.85 KB

associating_interceptors_with_classes_and_methods_using_the_interceptors_annotation.adoc

File metadata and controls

76 lines (63 loc) · 2.85 KB

Associating Interceptors with Classes and Methods using the Interceptors Annotation

The Interceptors annotation can be used to denote interceptor classes and associate one or more interceptor classes with a target class, and/or one or more of its methods, and/or a constructor of the target class.

The Interceptors annotation can be applied to the target class or to a method or a constructor declared in the target class or in a superclass of the target class:

Method-level around-invoke and around-timeout interceptors can be defined by applying the Interceptors annotation to the method for which the around-invoke or around-timeout interceptor methods are to be invoked.

Constructor-level interceptors can be defined by applying the Interceptors annotation to the constructor for which the around-construct interceptor methods are to be invoked.

Constructor- and method-level interceptors are invoked in addition to any interceptors declared in the target class, in an interceptor class associated with the target class, or in a superclass of the target class or interceptor class, and in addition to any default interceptors (if supported).

If multiple interceptor classes are specified in the Interceptors annotation, the interceptor methods of these classes are invoked in the order in which the classes are specified. The ordering rules for interceptors are defined in Chapter [interceptor_ordering].

The Interceptor annotation is ignored during the processing of classes bound using the Interceptors annotation. It will continue to be observed on such classes when used in the context of interceptor binding.

An extension specification may support the use of a deployment descriptor to associate interceptor classes with a target class, and/or method or constructor of a target class, and to specify the order of interceptor invocation or override metadata specified by annotations.

In the following example, the around-invoke methods specified by both the MyInterceptor and the MyOtherInterceptor classes will be invoked when the otherMethod method is called. The rules for ordering these interceptors are defined in Chapter [interceptor_ordering].

@Stateless
@Interceptors(org.acme.MyInterceptor.class)
public class MyBean {
    ...

    public void someMethod() {
        ...
    }

    @Interceptors(org.acme.MyOtherInterceptor.class)
    public void otherMethod() {
        ...
    }
}