Spring AOP方法拦截器与方法建议

时间:2015-06-25 14:20:21

标签: spring spring-aop

我是AOP的新手,我正在尝试理解Method Interceptor和MethodAdvice(即MethodBeforeAdvice或MethodAfterAdvice)之间的区别。对我来说,看起来两者都在做同样的事情,即调用方法调用。我们什么时候应该使用MethodInterceptor和MethodAdvice。

1 个答案:

答案 0 :(得分:1)

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.