Spring AOP Method Interceptor vs Method Advice

2.8k views Asked by At

I am new to AOP and I am trying to understand the difference between Method Interceptor and MethodAdvice(i.e. MethodBeforeAdvice or MethodAfterAdvice). To me looks like both are doing the same thing i.e. are called on method invocation. When should we use MethodInterceptor vs MethodAdvice.

1

There are 1 answers

0
pklndnst On

Take a look at the definition of the org.aopalliance.interceptInterceptor interface (implemented by MethodInterceptor):

public interface Interceptor extends Advice {
}

It's easy to see that a MethodInterceptor actually IS an Advice. The only difference between an Advice being defined in an @Aspect class and such an Interceptor is that Interceptor implementations can be added to and removed from Spring AOP Proxies at runtime (casting them to 'Advised'), whereas the Advice you're talking about is a more static construct. But their still essential to Spring AOP since their presence tells Spring which beans to wrap in a proxy object during application context startup.