Spring AOP之PointCut

实际上,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的类结构图 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值