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();
}