实际上,AOP的JointPoint可以有多种类型,但是Spring目前只支持方法级别的JoinPoint,(Keep It Simple,Stupid)。
Spring中定义了org.springframework.aop.PointCut作为切入点的顶级接口,它的定义如下:
public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
Pointcut TRUE = TruePointcut.INSTANCE;
}
其中ClassFilter用对类进行匹配它的定义如下:
public interface ClassFilter {
boolean matches(Class clazz);
ClassFilter TRUE = TrueClassFilter.INSTANCE;
}
若希望对某个当前传入的类进行织入,则使matches的方法返回true。
MethodMatcher用于对方法进行匹配,它的定义如下:
public interface MethodMatcher {
boolean matches(Method method, Class targetClass);
boolean isRuntime();
boolean matches(Method method, Class targetClass, Object[] args);
MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;
}
这个接口中提供了两个matches方法,
其中
boolean matches(Method method, Class targetClass);
这个方法只关心当前的类名和方法,并不关心传入的参数。
boolean matches(Method method, Class targetClass, Object[] args);
这个方法是对上面方法的增强,不但关心当前的类名和方法,还关心传入的参数。
这两个方法的分界点是isRuntime(),在前一种情况下,matches方法只考虑类名和方法名,而不考虑具体的参数,这种方法称为StaticMethodMather,由于对每个类的匹配结果可以采用缓存进行优化,提高效率。
而后面的情况由于每次都需要传入方法的具体参数,称为DynamicMethodMatcher,由于无法采用缓存,因此性能比前面的要差。
在StaticMethodMather和DynamicMethodMatcher的区别下,因此Spring的PointCut也可以分为两类StaticMethodMatcherPointcut和DynamicMethodMatcherPointcut,下图是Spring中Pointcut的类结构图