Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

June 11, 2010

Around advice vs Before/ After Advice

MethodInterceptor provides the ability to do both berfore and after advices in one advice object:
public interface MethodInterceptor extends Interceptor {
Object invoke(MethodInvocation invocation) throws Throwable;
}
There are two important differences between the MethodInterceptor interface
and the previous two types of advice. First, the MethodInterceptor implementation
controls whether the target method is actually invoked. Invoking the target
method is done by calling MethodInvocation.proceed(). This is in contrast to
MethodBeforeAdvice, where the target method is always called unless you throw
an exception.
Second, MethodInterceptor gives you control over what object is returned. This
means you can return a completely different object than the one returned by proceed().
Remember, with AfterReturningAdvice you had access to the object being returned, but you could not return a different object. While MethodInterceptor
provides this added flexibility, you should use caution when returning a different
object than the one returned by the target method and only do so when necessary.

No comments: