In my Java EE program I want to use Interceptor for logging purpose. It's easy to use when I'm entering a method :
The annotation :
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({ METHOD, TYPE })
public @interface Logged {
}
The interceptor :
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Logger logger;
@AroundInvoke
public Object logMethodEntry(InvocationContext invocationContext) throws Exception {
logger.info("Entering method: "
+ invocationContext.getMethod().getName() + " in class "
+ invocationContext.getMethod().getDeclaringClass().getName());
return invocationContext.proceed();
}
}
My class using the interceptor :
public class MyClass {
@Logged
public void MyMethod() {
// do something
}
}
But now I want to do same thing when I'm leaving MyMethod. Is that possible ?