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