Advice
Advice
是 AOP 聯盟中所有增強(通知)類型的標記接口,表示可以被織入目標對象的橫切邏輯,例如前置通知、后置通知、異常通知、攔截器等。
package org.aopalliance.aop;public interface Advice {}
BeforeAdvice
前置通知的標記接口,表示增強邏輯應在目標方法調用之前執行。
public interface BeforeAdvice extends Advice {}
MethodBeforeAdvice
在目標方法執行前調用的通知接口,可訪問方法、參數和目標對象。
public interface MethodBeforeAdvice extends BeforeAdvice {void before(Method method, Object[] args, @Nullable Object target) throws Throwable;
}
AfterAdvice
后置通知的標記接口,用于標識在方法執行完成后執行的通知。
public interface AfterAdvice extends Advice {}
AfterReturningAdvice
在目標方法正常返回后執行的通知接口,能獲取返回值及方法信息。
public interface AfterReturningAdvice extends AfterAdvice {void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable;
}
ThrowsAdvice
異常通知接口,定義在目標方法拋出異常后執行的增強邏輯(通過約定方法名實現,非標準接口方法)。
public interface ThrowsAdvice extends AfterAdvice {}
DynamicIntroductionAdvice
用于判斷當前通知是否引入了某個接口,實現 AOP 中的動態引入(引介增強)。
public interface DynamicIntroductionAdvice extends Advice {boolean implementsInterface(Class<?> intf);
}
Interceptor
Interceptor 是 AOP 聯盟中用于表示通用運行時攔截器的接口,繼承自 Advice,其子接口用于具體攔截方法、構造函數、字段訪問等連接點。
public interface Interceptor extends Advice {}
MethodInterceptor
MethodInterceptor
是實現方法環繞通知的接口,允許在目標方法調用前后執行自定義邏輯,并決定是否調用目標方法。
@FunctionalInterface
public interface MethodInterceptor extends Interceptor {@NullableObject invoke(@Nonnull MethodInvocation invocation) throws Throwable;}// `MethodInvocation` 表示方法調用的描述,是一種連接點,允許方法攔截器在方法調用時進行攔截并獲取被調用的方法信息。
public interface MethodInvocation extends Invocation {@NonnullMethod getMethod();}// `Invocation` 表示程序中的一次調用,它是一個可被攔截器攔截的連接點,并提供訪問和修改參數的能力。
public interface Invocation extends Joinpoint {@NonnullObject[] getArguments();}// `Joinpoint` 表示程序執行中的一個運行時連接點,是攔截器鏈中可被處理的事件,提供繼續執行、獲取目標對象和靜態連接點信息的方法。
public interface Joinpoint {@NullableObject proceed() throws Throwable;@NullableObject getThis();@NonnullAccessibleObject getStaticPart();}
IntroductionInterceptor
IntroductionInterceptor
是 Spring AOP 中實現引介(Introduction)功能的關鍵接口。通過它,您可以為代理對象動態引入額外的接口,并實現類似多重繼承的效果。它通常用于解決跨多個類注入統一功能(如日志、事務等)的需求,同時保持原始對象的代碼不變。
public interface IntroductionInterceptor extends MethodInterceptor, DynamicIntroductionAdvice {}
Advisor
Advisor
接口是 Spring AOP 中封裝 Advice(通知) 及其適用條件(如切點過濾器)的基礎接口。它的設計目的是統一支持不同類型的通知,如環繞通知(around advice)、前置通知(before advice)、異常通知(throws advice)等。
public interface Advisor {Advice EMPTY_ADVICE = new Advice() {};Advice getAdvice();default boolean isPerInstance() {return true;}}
IntroductionAdvisor
IntroductionAdvisor
接口繼承了 Advisor
和 IntroductionInfo
,用于定義引入(Introduction)增強,提供過濾目標類的能力(通過 getClassFilter
方法)和驗證接口的功能(通過 validateInterfaces
方法)。IntroductionInfo
接口則提供了獲取引入接口列表的功能。
public interface IntroductionAdvisor extends Advisor, IntroductionInfo {ClassFilter getClassFilter();void validateInterfaces() throws IllegalArgumentException;}
public interface IntroductionInfo {Class<?>[] getInterfaces();}
PointcutAdvisor
PointcutAdvisor
是所有基于切點(Pointcut)驅動的通知者(Advisor)的超接口,定義了獲取驅動該通知者的切點的方法。它適用于絕大多數通知者,除了不涉及方法級匹配的引入通知者(introduction advisors)
public interface PointcutAdvisor extends Advisor {Pointcut getPointcut();}
Pointcut
Pointcut
是 Spring AOP 的核心抽象,表示切點,由 ClassFilter
(類過濾器)和 MethodMatcher
(方法匹配器)組成,用于定義在哪些類和方法上應用通知,且可以組合多個切點,TRUE
是一個永遠匹配的切點實例。
public interface Pointcut {ClassFilter getClassFilter();MethodMatcher getMethodMatcher();Pointcut TRUE = TruePointcut.INSTANCE;
}
ExpressionPointcut
ExpressionPointcut
是一個接口,要求實現的切點使用字符串表達式,并提供了一個方法來返回該表達式。
public interface ExpressionPointcut extends Pointcut {@NullableString getExpression();}
ClassFilter
ClassFilter
是用于限制切點或引入通知僅匹配特定目標類的過濾器接口,定義了判斷一個類是否匹配的方法 matches(Class<?> clazz),并要求實現類正確重寫 equals、hashCode 和 toString,以支持緩存和代理生成。
@FunctionalInterface
public interface ClassFilter {boolean matches(Class<?> clazz);ClassFilter TRUE = TrueClassFilter.INSTANCE;}
MethodMatcher
MethodMatcher
接口用于判斷目標方法是否符合通知條件,支持靜態匹配(只基于方法和類信息)和動態匹配(基于方法參數等運行時信息)。如果 isRuntime()
返回 false
,只會調用靜態匹配方法;如果返回 true
,則在每次調用前執行動態匹配。實現類需重寫 equals
、hashCode
和 toString
以支持緩存和代理場景。
public interface MethodMatcher {boolean matches(Method method, Class<?> targetClass);boolean isRuntime();boolean matches(Method method, Class<?> targetClass, Object... args);MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;}
IntroductionAwareMethodMatcher
IntroductionAwareMethodMatcher
是一種擴展了 MethodMatcher
的接口,支持在匹配方法時考慮引入(introductions),以優化匹配過程。
public interface IntroductionAwareMethodMatcher extends MethodMatcher {boolean matches(Method method, Class<?> targetClass, boolean hasIntroductions);}
InstantiationModelAwarePointcutAdvisor
InstantiationModelAwarePointcutAdvisor
接口用于支持懶加載初始化策略的 Advisor
,允許在真正需要時才實例化 Advice
,提高性能和靈活性。
public interface InstantiationModelAwarePointcutAdvisor extends PointcutAdvisor {boolean isLazy();boolean isAdviceInstantiated();
}