• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
斑駁_光影
                              Keep Hunger     Keep Foolish
博客园    首页    新随笔    联系   管理    订阅  订阅

spring AoP学习 -----AoP的相关概念实体Joinpoint / pointcut

  Joinpoint:在SpringAoP中,只支持Method Execution (方法执行)的Joinpoint,对于类型中的属性,我们可以通过对setter,getter方法的拦截从而达到相同的效果。

  Pointcut: spring AoP以接口定义 aop.PointCut作为其AoP框架中说有 PointCut的最顶层抽象,该接口提供了两个方法用来帮助捕捉JoinPoint,并提供了一个TruePointcut类型实例。PointCut提供了三个方法:

public interface Pointcut {

    /**
     * Return the ClassFilter for this pointcut.
     * @return the ClassFilter (never <code>null</code>)
     */
    ClassFilter getClassFilter();

    /**
     * Return the MethodMatcher for this pointcut.
     * @return the MethodMatcher (never <code>null</code>)
     */
    MethodMatcher getMethodMatcher();


    /**
     * Canonical Pointcut instance that always matches.
     */
    Pointcut TRUE = TruePointcut.INSTANCE;

}

 

其中ClassFilter用于匹配将被织入操作的对象,MethodMatcher用于匹配将被织入操作的方法,ClassFilter中提供了两个方法:

public interface Pointcut {

    /**
     * Return the ClassFilter for this pointcut.
     * @return the ClassFilter (never <code>null</code>)
     */
    ClassFilter getClassFilter();

    /**
     * Return the MethodMatcher for this pointcut.
     * @return the MethodMatcher (never <code>null</code>)
     */
    MethodMatcher getMethodMatcher();


    /**
     * Canonical Pointcut instance that always matches.
     */
    Pointcut TRUE = TruePointcut.INSTANCE;

}

 

MethodMatcher则比较繁杂,提供了4个方法:

public interface Pointcut {

    /**
     * Return the ClassFilter for this pointcut.
     * @return the ClassFilter (never <code>null</code>)
     */
    ClassFilter getClassFilter();

    /**
     * Return the MethodMatcher for this pointcut.
     * @return the MethodMatcher (never <code>null</code>)
     */
    MethodMatcher getMethodMatcher();


    /**
     * Canonical Pointcut instance that always matches.
     */
    Pointcut TRUE = TruePointcut.INSTANCE;

}

其中,当 isRuntime为false时,只执行两个参数的matches,忽略参数去进行匹配,这种类型的MethodMatcher称为StaticMethodMatcher,因为不用每次都去匹配结构,就可以在框架内部缓存以提高性能。当isRuntime为true时,表明会对方法调用的参数进行匹配检查,称之为DynamicMethodMatcher,由于无法进行缓存,所以效率较差。若第一个matches方法执行完返回false,这后面都将不会执行。

 

  常见Pointcut:

  1. NameMatchMethodPointcut:最简单的Pointcut实现,属于staticMethodMatcherPointcut的子类,可以根据自身指定的一组方法名称与Joinpoint处的方法的方法名称进行匹配,可以使用“*”进行简单的模糊匹配。
  2. JdkRegexpMethodPoint和Perl15RegexpMethodPoint属于StaticMethodMatcherPointcut的子类,专门提供基于正则表达式的实现分支,其中第一个的实现是基于JDK1.4之后引入的JDK标准正则表达式。第二种是基于Perl15风格的正则表达式。
  3. AnnontationMatchingPointcut:使用注解来匹配JoinPoint
  4. ComposablePointcut:实现Pointcut的逻辑运算功能
  5. ControlFlowPointcut:用于实现拦截被特定对象调用的joinpoint

  

posted @ 2012-07-24 17:01  斑驳_光影  阅读(7028)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3