Advisor接口及其实现类是Advice(通知)和PointCut(切入点)的一个组合体,按照aop的定义其就是一个Aspect切面。
Advisor及其实现类:
在接口Advisor中定义了获取Advice通知的方法
public interface Advisor {
//获取通知
Advice getAdvice();
boolean isPerInstance();
}
在PointcutAdvisor中定义了获取PointCut的方法
public interface PointcutAdvisor extends Advisor {
//获取切入点
Pointcut getPointcut();
}
简单来说Advisor的实现类就是一个包含了Advice(通知)和PointCut(切入点)的数据结构。
我们来分析一下Spring配置文件中配置aop的配置属性。
<aop:config>
<aop:aspect ref="transaction">
<aop:before method="startTransaction" pointcut="execution(* *.save(..))"/>
<aop:after method="commitTransaction" pointcut="execution(* *.save(..))"/>
<aop:after-throwing method="rollbackTransaction" pointcut="execution(* *.save(..))"/>
<aop:after-returning method="commitTransaction" pointcut="execution(* *.save(..))"/>
</aop:aspect>
</aop:config>
其中:
1、<aop:aspect>代表一个切面Advisor集合
2、ref=“transaction” 代表了一个通知的集合,里面可能会有多个通知方法
3、pointcut 代表切入点的匹配表达式
4、before、after等的method表示每个通知
5、before加上method加上pointcut就可以作为一个Advisor切面
这样通过pointcut的匹配表达式,可以将Advice横切到目标类的方法中