Java Reference
In-Depth Information
The combination of @AroundInvoke and @Interceptors makes providing powerful
crosscutting concerns to your application using EJB 3 interceptors simple and easy. This
was just a quick taste of how EJB 3 interceptors are implemented to get you going. In sec-
tion 5.3.6 we'll provide a more comprehensive example and explanation of EJB 3 inter-
ceptor features. But first we'll talk more about the different options available in EJB 3 for
specifying interceptors.
5.3.5. Specifying interceptors
Now that you know the annotations involved for interceptors, you'll learn how to use them.
Like a lot of annotations, @Interceptors can be used at both a method and a class
level. So you'll see examples of this. Annotations are not all-powerful, however. In some
cases annotations cannot do the job. This is true for @Interceptors , and you'll see an
example of this where you'll need to abandon annotations and use the ejb-jar.xml file again.
Method- and class-level interceptors
The @Interceptors annotation allows you to specify one or more interceptor classes
for a method or class. In listing 5.5 a single interceptor is attached to the findOrder-
ById() method. In this example, the sayHello() method of SayHelloInter-
ceptor will be called before findOrderById() is called:
public Order findOrderById(String id) { ... }
In listing 5.6 a single interceptor is attached to the entire OrderBean class. When you
attach an interceptor to a class, the interceptor is triggered if any of the target class's meth-
ods are invoked. In this example the sayHello() method of SayHelloIntercept-
or will be called before any of the methods in OrderBean are called:
public class OrderBean { ... }
The @Interceptors annotation is fully capable of attaching more than one interceptor
at either a class or a method level. All you have to do is provide a comma-separated list
as a parameter to the annotation. For example, add two interceptors to OrderBean at the
class level:
Search WWH ::

Custom Search