Java Reference
In-Depth Information
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@LoggingInterceptorBinding
@Interceptor
public class LoggingInterceptor {
private static final Logger logger = Logger.getLogger(
LoggingInterceptor.class.getName());
@AroundInvoke
public Object logMethodCall(InvocationContext invocationContext)
throws Exception {
logger.log(Level.INFO, new StringBuilder("entering ").append(
invocationContext.getMethod().getName()).append(
" method").toString());
Object retVal = invocationContext.proceed();
logger.log(Level.INFO, new StringBuilder("leaving ").append(
invocationContext.getMethod().getName()).append(
" method").toString());
return retVal;
}
}
As we can see, other than being annotated with our interceptor binding type, the
above class is a standard interceptor, just like the ones we use with EJB session beans
(refer to Chapter 7 for details).
In order for our interceptor binding type to work properly, we need to register the
above interceptor in beans.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>com.ensode.interceptor.LoggingInterceptor</class>
</interceptors>
</beans>
 
Search WWH ::




Custom Search