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