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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM