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.
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.
Top Navigation Menu
Showing posts with label Advice. Show all posts
Showing posts with label Advice. Show all posts
June 11, 2010
Advice in Spring AOP
Advice contains the logic of your aspect. So,when you create an advice object, you are writing the code that implements the
cross-cutting functionality. Also, remember that Spring’s joinpoint model is built
around method interception. This means that the Spring advice you write will be
woven into your application at different points around a method’s invocation.
Because there are several points during the execution of a method that Spring
can weave in advice, there are several different advice types.
Advice type Interface Description
Around org.aopalliance.intercept.MethodInterceptor Intercepts calls to the target
method
Before org.springframework.aop.BeforeAdvice Called before the target
method is invoked
After org.springframework.aop.AfterReturningAdvice Called after the target
method returns
Throws org.springframework.aop.ThrowsAdvice Called when target
method throws an
exception
cross-cutting functionality. Also, remember that Spring’s joinpoint model is built
around method interception. This means that the Spring advice you write will be
woven into your application at different points around a method’s invocation.
Because there are several points during the execution of a method that Spring
can weave in advice, there are several different advice types.
Advice type Interface Description
Around org.aopalliance.intercept.MethodInterceptor Intercepts calls to the target
method
Before org.springframework.aop.BeforeAdvice Called before the target
method is invoked
After org.springframework.aop.AfterReturningAdvice Called after the target
method returns
Throws org.springframework.aop.ThrowsAdvice Called when target
method throws an
exception
Subscribe to:
Posts (Atom)