Java Reference
In-Depth Information
public @interface Logged {
}
The
LoggedInterceptor
class looks like this:
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
...
public LoggedInterceptor() {
}
@AroundInvoke
public Object logMethodEntry(InvocationContext invocationCon-
text)
throws Exception {
System.out.println("Entering method: "
+ invocationContext.getMethod().getName() + " in
class "
+ invocationCon-
text.getMethod().getDeclaringClass().getName());
return invocationContext.proceed();
}
}
The class is annotated with both the
@Logged
and the
@Interceptor
annotations.
The
@AroundInvoke
method,
logMethodEntry
, takes the required
Invoca-
tionContext
argument, and calls the required
proceed
method. When a method is
intercepted,
logMethodEntry
displays the name of the method being invoked as well
as its class.
To enable the interceptor, the
beans.xml
file defines it as follows:
<interceptors>
<class>billpayment.interceptor.LoggedInterceptor</class>
</interceptors>