
概述
在工作中用的最多的就是通過(guò)@Aspect實(shí)現(xiàn)AOP功能;要在Spring配置中使用@Aspect切面,需要啟用Spring支持,以便基于@Aspect切面配置Spring AOP,并根據(jù)條件自動(dòng)代理bean。通過(guò)自動(dòng)代理,如果Spring確定某個(gè)bean符合一個(gè)或多個(gè)切面的建議,它會(huì)自動(dòng)為該bean生成一個(gè)代理來(lái)攔截方法調(diào)用,并確保按需運(yùn)行通知。
可以通過(guò)XML或java風(fēng)格的配置啟用@AspectJ支持。在這兩種情況下,還需要確保AspectJ的aspectjweaver.jar庫(kù)位于應(yīng)用程序的類路徑上(版本1.8或更高)。
通過(guò)注解方式開(kāi)啟@Aspect支持
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
}
通過(guò)XML開(kāi)啟@Aspect支持。
定義AspectJ切面。
package com.pack.aspect;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CustomAspect {
  // 定義切入點(diǎn)
  @Pointcut("execution(* com.pack.service..*.(..))")
  private void log() {}
  // 定義通知
  @Before("log()")
  // @Before("execution(* com.pack.service..*.(..))")  也可以直接這樣寫
  public void recordLogBefore() {  
    // ...
  }
  @AfterReturning("log()")
  public void recordLogAfter(){
    // ...
  }
}
上面簡(jiǎn)單回顧了在工作中使用@Aspect定義切面實(shí)現(xiàn)AOP功能。
Spring AOP API
Spring的切入點(diǎn)模型支持獨(dú)立于通知類型的切入點(diǎn)重用??梢允褂孟嗤那腥朦c(diǎn)定位不同的通知。
pointcut接口是中心接口,用于為特定類和方法提供建議。完整的接口如下:
public interface Pointcut {
  ClassFilter getClassFilter();
  MethodMatcher getMethodMatcher();
}將切入點(diǎn)接口拆分為兩個(gè)部分允許重用類和方法匹配部分以及細(xì)粒度的組合操作。
ClassFilter接口用于將切入點(diǎn)限制為給定的目標(biāo)類集。如果matches()方法總是返回true,則匹配所有目標(biāo)類。ClassFilter接口的定義如下列代碼清單所示:
public interface ClassFilter {
  boolean matches(Class clazz);
}該類專門用來(lái)匹配每一個(gè)Bean是否符合條件,只有匹配了才可為其創(chuàng)建代理。
MethodMatcher接口通常更重要。完整的接口如下:
public interface MethodMatcher {
  /**
   * 在運(yùn)行目標(biāo)類的方法時(shí)判斷當(dāng)前的執(zhí)行的方法是否匹配,如果匹配才會(huì)執(zhí)行
   * 相關(guān)的通知
   */
  boolean matches(Method m, Class<?> targetClass);
  /**
   * 上面2個(gè)參數(shù)的matches返回true才會(huì)執(zhí)行isRuntime
   * 該方法的返回值決定了下面3個(gè)參數(shù)的matches方法是否會(huì)被執(zhí)行
   * 如果返回true,才會(huì)進(jìn)行下面3個(gè)參數(shù)的執(zhí)行。返回false將不會(huì)執(zhí)行下面方法
   */
  boolean isRuntime();
  /**
   * 該方法是否會(huì)被執(zhí)行是由上面的isRuntime方法決定,只有返回true才會(huì)執(zhí)行
   * 如果isRuntime方法返回true,那么會(huì)將每一個(gè)Advisor中定義的通知
   * (這些通知會(huì)被轉(zhuǎn)換為MethodInterceptor)包裝為InterceptorAndDynamicMethodMatcher
   * 最后在通過(guò)ReflectiveMethodInvocation執(zhí)行時(shí)會(huì)判斷當(dāng)前對(duì)象如果為ReflectiveMethodInvocation
   * 則進(jìn)行MethodMatcher3個(gè)參數(shù)的matches調(diào)用,這里就可以對(duì)參數(shù)進(jìn)行相應(yīng)的校驗(yàn)判斷,
   * 是否進(jìn)行通知的繼續(xù)調(diào)用,如果匹配則調(diào)用當(dāng)前的MethodInterceptor,否則直接調(diào)用下一個(gè)
   */
  boolean matches(Method m, Class<?>Spring支持切入點(diǎn)上的操作(特別是union和intersection)。Union表示任意一個(gè)切入點(diǎn)匹配的方法。交集意味著兩個(gè)切入點(diǎn)匹配的方法。Union通常更有用??梢允褂胦rg.springframework.aop.support.Pointcuts類中的靜態(tài)方法來(lái)組合切入點(diǎn),也可以使用同一個(gè)包中的 ComposablePointcut類。然而,使用AspectJ切入點(diǎn)表達(dá)式通常是一種更簡(jiǎn)單的方法。
Union表示了多個(gè)Pointcut都需要匹配才算匹配。
public abstract class Pointcuts {
  public static Pointcut union(Pointcut pc1, Pointcut pc2){
    return new ComposablePointcut(pc1).union(pc2);
  }
}ComposablePointcut
public class ComposablePointcut implements Pointcut, Serializable {
  private ClassFilter classFilter;
  private MethodMatcher methodMatcher;
  public ComposablePointcut(Pointcut pointcut) {
    this.classFilter = pointcut.getClassFilter();
    this.methodMatcher = pointcut.getMethodMatcher();
  }
  public ComposablePointcut union(Pointcut other) {
    this.methodMatcher = MethodMatchers.union(this.methodMatcher, this.classFilter, other.getMethodMatcher(), other.getClassFilter());
    this.classFilter = ClassFilters.union(this.classFilter, other.getClassFilter());
    return this;
  }
}MethodMatchers.union
static MethodMatcher union(MethodMatcher mm1, ClassFilter cf1, MethodMatcher mm2, ClassFilter cf2){
  return (mm1 instanceof IntroductionAwareMethodMatcher || mm2 instanceof IntroductionAwareMethodMatcher ?
          new ClassFilterAwareUnionIntroductionAwareMethodMatcher(mm1, cf1, mm2, cf2) :
          new ClassFilterAwareUnionMethodMatcher(mm1, cf1, mm2, cf2));
}如上假設(shè)返回ClassFilterAwareUnionMethodMatcher。
private static class ClassFilterAwareUnionMethodMatcher extends UnionMethodMatcher {
  private final ClassFilter cf1;
  private final ClassFilter cf2;
  public ClassFilterAwareUnionMethodMatcher(MethodMatcher mm1, ClassFilter cf1, MethodMatcher mm2, ClassFilter cf2){
    super(mm1, mm2);
    this.cf1 = cf1;
    this.cf2 = cf2;
  }
}
private static class UnionMethodMatcher implements MethodMatcher, Serializable {
  // 最終的核心就是分別判斷兩個(gè)Pointcut對(duì)應(yīng)的ClassFilter,MethodMatcher
  // 只要其中一個(gè)返回true即可
  public boolean matches(Method method, Class<?> targetClass){
    return (matchesClass1(targetClass) && this.mm1.matches(method, targetClass)) || 
      (matchesClass2(targetClass) && this.mm2.matches(method, targetClass));
  }
}- 方便的切入點(diǎn)實(shí)現(xiàn)
 
Spring為我們提供了幾個(gè)便捷的切入點(diǎn)實(shí)現(xiàn)類可以直接使用。
靜態(tài)切入點(diǎn),靜態(tài)切入點(diǎn)基于方法和目標(biāo)類,不能考慮方法的參數(shù)。對(duì)于大多數(shù)用法,靜態(tài)切入點(diǎn)就足夠了,而且是最好的。Spring只能在方法第一次被調(diào)用時(shí)對(duì)靜態(tài)切入點(diǎn)進(jìn)行一次評(píng)估。之后,就不需要對(duì)每個(gè)方法調(diào)用再次評(píng)估切入點(diǎn)了。
正則表達(dá)式切點(diǎn)
指定靜態(tài)切入點(diǎn)的一個(gè)明顯方法是正則表達(dá)式。除了Spring之外,還有幾個(gè)AOP框架使之成為可能。org.springframework.aop.support.JdkRegexpMethodPointcut是一個(gè)通用正則表達(dá)式切入點(diǎn),它使用JDK中的正則表達(dá)式支持。使用JdkRegexpMethodPointcut類,可以提供一組模式字符串。如果其中任何一個(gè)匹配,切入點(diǎn)計(jì)算為true。
<bean id="staticRegexpPoint"class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  <property name="patterns">
    <list>
      <value>.*set.*</value>
      <value>.*save</value>
    </list>
  </property>
  <property name="advice">
    <ref bean="logAdvice"/>
  </property>
</bean>
動(dòng)態(tài)切入點(diǎn)
動(dòng)態(tài)切入點(diǎn)的評(píng)估成本比靜態(tài)切入點(diǎn)高。它們既考慮了方法參數(shù),也考慮了靜態(tài)信息。這意味著每次方法調(diào)用都必須計(jì)算它們,而且結(jié)果不能緩存,因?yàn)閰?shù)不同。
核心切入點(diǎn)類:ControlFlowPointcut?。
public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher, Serializable {
  private final Class<?> clazz;
  private final String methodName;
  /**
   * 構(gòu)造一個(gè)新的切入點(diǎn),它匹配給定類中給定方法下面的所有調(diào)用。
   * 如果沒(méi)有給出方法名,則匹配給定類下的所有控制流。
    */
  public ControlFlowPointcut(Class<?> clazz, @Nullable String methodName) {
    this.clazz = clazz;
    this.methodName = methodName;
  }
  public boolean matches(Method method, Class<?> targetClass) {
    return true;
  }
  @Override
  public boolean isRuntime() {
    return true;
  }
  @Override
  public boolean matches(Method method, Class<?> targetClass, Object... args) {
    // 遍歷當(dāng)前的執(zhí)行棧中的所有方法是否有匹配當(dāng)前在構(gòu)造方法中傳入的方法名,有則進(jìn)行相應(yīng)的通知調(diào)用
    // 簡(jiǎn)單點(diǎn)說(shuō)就是:攔截任何被創(chuàng)建代理類的方法,如果這些方法在執(zhí)行過(guò)程中有調(diào)用構(gòu)造參數(shù)中傳入的Class 和Method那么就是匹配的
    for (StackTraceElement element : new Throwable().getStackTrace()) {
      if (element.getClassName().equals(this.clazz.getName()) && 
          (this.methodName == null || element.getMethodName().equals(this.methodName))) {
        return true;
      }
    }
    return false;
  }
}Pointcut超類?
Spring提供了有用的切入點(diǎn)超類來(lái)幫助您實(shí)現(xiàn)自己的切入點(diǎn)。
因?yàn)殪o態(tài)切入點(diǎn)最有用,你可能應(yīng)該子類化StaticMethodMatcherPointcut。這只需要實(shí)現(xiàn)一個(gè)抽象方法(盡管你可以覆蓋其他方法來(lái)定制行為)。下面的例子展示了如何子類化StaticMethodMatcherPointcut:
public class CustomStaticPointcut extends StaticMethodMatcherPointcut {
  public boolean matches(Method m, Class targetClass){
    // return true if custom criteria match
  }
}