Java Reference
In-Depth Information
The following example illustrates a simple interceptor class:
package com.ensode.sessionbeanintro.ejb;
import java.lang.reflect.Method;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class LoggingInterceptor {
@AroundInvoke
public Object logMethodCall(
InvocationContext invocationContext)
throws Exception {
Object interceptedObject =
invocationContext.getTarget();
Method interceptedMethod =
invocationContext.getMethod();
System.out.println("Entering " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
Object o = invocationContext.proceed();
System.out.println("Leaving " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
return o;
}
}
The preceding example sends a message to the application server log just before
and just after an intercepted method is executed. The purpose of implementing
something like this would be to aid in debugging applications.
For simplicity, our example simply uses System.out.
println to output messages to the application server log. A real
application more than likely would use a logging API such as the
Java Logging API or Log4j.
 
Search WWH ::




Custom Search