Java Reference
In-Depth Information
The first thing that we do in our interceptor method is obtain a reference to the
object and method being intercepted. We then output a message to the log indicating
the class and method being invoked. This code is executed just before we let the
intercepted method execute, which we do by invoking invocationContext.
proceed() . We store the return value of this method in a variable, and then add
some additional logic to be executed just after the method finishes. In our example,
we simply send an additional line of text to the application server log. Finally, our
method returns the return value of invocationContext.proceed() .
Decorating the EJB with the @Interceptors
For an EJB's method to be intercepted, it must be decorated with the @Interceptors
annotation, which has a single class array attribute. This attribute contains all the
interceptors to be executed before and/or after the method call.
The @Interceptors annotation can be used at the method level, in which case it
applies only to the method it decorates, or at the class level, in which it applies
to every method in the bean.
The following example is a new version of our EchoBean session bean,
which is slightly modified to have its echo() method intercepted by the
LoggingInterceptor class we wrote in the previous section:
package com.ensode.sessionbeanintro.ejb;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.interceptor.Interceptors;
public class Echo implements EchoRemote {
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public String echo(String saying) {
return "echoing: " + saying;
Search WWH ::

Custom Search