Java Reference
In-Depth Information
the method or take a different execution path. Now that you know when to use interceptors,
let's look at how a basic logging interceptor is implemented.
5.3.4. How interceptors are implemented
Implementing an EJB 3 interceptor requires only two annotations. The first is @Around-
Invoke . This annotation is placed on the method of a class that you want to serve as the
interceptor's advice. Remember, an advice is the actual code you want to weave into your
session bean or MDB business methods. The following listing is a quick example. We'll go
into more detail about what this code is doing in section 5.3.6 .
Listing 5.4. Annotate method with @AroundInvoke
public class SayHelloInterceptor {
public Object sayHello(InvocationContext ctx) throws Exception {
System.out.println("Hello Interceptor!");
return ctx.proceed();
The second annotation is @Interceptors . This annotation is used in the EJB or MDB
class and defines how interceptors are applied to the methods of the class. The next listing
shows the @Interceptors annotation applied to an individual method.
Listing 5.5. Annotate EJB method with @Interceptors
public class OrderBean {
public Order findOrderById(String id) { return null; }
The following listing shows the @Interceptors annotation applied to the EJB class it-
Listing 5.6. Annotate EJB class with @Interceptors
public class OrderBean { }
Search WWH ::

Custom Search