此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
Spring 中的 Pointcut API
本节描述了 Spring 如何处理关键的切入点概念。
概念
Spring 的切入点模型支持独立于通知类型的切入点重用。您可以 使用相同的切入点定位不同的建议。
这org.springframework.aop.Pointcut
interface 是中央接口,用于
将 Advice 定位到特定的类和方法。完整的界面如下:
public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}
拆分Pointcut
接口分为两部分,允许重用类和方法
匹配部分和精细组合作(例如执行 “union”
替换为另一个方法 matcher)。
这ClassFilter
interface 用于将切入点限制为给定的目标集
类。如果matches()
method 始终返回 true,则所有目标类都是
匹配。下面的清单显示了ClassFilter
接口定义:
public interface ClassFilter {
boolean matches(Class clazz);
}
这MethodMatcher
interface 通常更重要。完整的界面如下:
public interface MethodMatcher {
boolean matches(Method m, Class<?> targetClass);
boolean isRuntime();
boolean matches(Method m, Class<?> targetClass, Object... args);
}
这matches(Method, Class)
method 用于测试此切入点是否曾经
匹配目标类上的给定方法。当 AOP
创建 proxy 是为了避免在每次方法调用时都需要进行测试。如果
双参数matches
method 返回true
对于给定方法,并且isRuntime()
method 的 MethodMatcher 返回true
,则三个参数 matches 方法是
在每次方法调用时调用。这允许切入点查看传递的参数
添加到紧接 Target 通知开始之前的方法调用。
最MethodMatcher
实现是静态的,这意味着它们的isRuntime()
方法
返回false
.在这种情况下,三个参数matches
method 永远不会被调用。
如果可能,请尝试将切入点设为静态,允许 AOP 框架缓存 创建 AOP 代理时的切入点评估结果。 |
对切入点的作
Spring 支持对切入点进行作(特别是 union 和 intersection)。
Union 表示任一切入点匹配的方法。
Intersection 表示两个切入点匹配的方法。
Union 通常更有用。
你可以使用org.springframework.aop.support.Pointcuts
类或使用ComposablePointcut
类。但是,使用 AspectJ 切入点
表达式通常是一种更简单的方法。
AspectJ 表达式切入点
从 2.0 开始,Spring 使用的最重要的切入点类型是org.springframework.aop.aspectj.AspectJExpressionPointcut
.这是一个切入点
使用 AspectJ 提供的库来解析 AspectJ 切入点表达式字符串。
有关支持的 AspectJ 切入点原语的讨论,请参见上一章。
便利的切入点实现
Spring 提供了几个方便的切入点实现。您可以使用其中的一些 径直;其他的旨在在特定于应用程序的切入点中被子类化。
静态切入点
静态切入点基于方法和目标类,不能考虑 方法的参数。对于大多数用法,静态切入点就足够了,而且是最好的。 Spring 只能在首次调用方法时对静态切入点进行一次求值。 之后,无需在每次方法调用时再次评估切入点。
本节的其余部分描述了一些静态切入点实现,这些实现是 包含在 Spring 中。
正则表达式切入点
指定静态切入点的一种明显方法是正则表达式。多个 AOP
除了 Spring 之外的框架使这成为可能。org.springframework.aop.support.JdkRegexpMethodPointcut
是泛型常规
expression 切入点,该切入点使用 JDK 中的正则表达式支持。
使用JdkRegexpMethodPointcut
类中,你可以提供模式字符串列表。
如果其中任何一个是匹配项,则切入点的计算结果为true
.(因此,
生成的切入点实际上是指定模式的并集。
以下示例演示如何使用JdkRegexpMethodPointcut
:
<bean id="settersAndAbsquatulatePointcut"
class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>.*set.*</value>
<value>.*absquatulate</value>
</list>
</property>
</bean>
Spring 提供了一个名为RegexpMethodPointcutAdvisor
,这让我们
还引用Advice
(请记住,Advice
可以是拦截器,在 advice 之前,
throws advice 等)。在幕后, Spring 使用JdkRegexpMethodPointcut
.
用RegexpMethodPointcutAdvisor
简化了布线,因为一个 bean 封装了
pointcut 和 advice 一起创建,如下例所示:
<bean id="settersAndAbsquatulateAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="beanNameOfAopAllianceInterceptor"/>
</property>
<property name="patterns">
<list>
<value>.*set.*</value>
<value>.*absquatulate</value>
</list>
</property>
</bean>
您可以使用RegexpMethodPointcutAdvisor
与任何Advice
类型。
切入点超类
Spring 提供了有用的切入点超类来帮助您实现自己的切入点。
因为静态切入点最有用,所以你可能应该子类化StaticMethodMatcherPointcut
.这只需要实现一个
abstract 方法(尽管您可以覆盖其他方法来自定义行为)。这
以下示例演示如何子类化StaticMethodMatcherPointcut
:
-
Java
-
Kotlin
class TestStaticPointcut extends StaticMethodMatcherPointcut {
public boolean matches(Method m, Class targetClass) {
// return true if custom criteria match
}
}
class TestStaticPointcut : StaticMethodMatcherPointcut() {
override fun matches(method: Method, targetClass: Class<*>): Boolean {
// return true if custom criteria match
}
}
还有用于动态切入点的超类。 您可以将自定义切入点与任何通知类型一起使用。
自定义切入点
因为 Spring AOP 中的切入点是 Java 类而不是语言特性(如 AspectJ),你可以声明自定义切入点,无论是静态的还是动态的。习惯 Spring 中的切入点可以是任意复杂的。但是,我们建议使用 AspectJ 切入点 表达式语言(如果可以)。
Spring 的更高版本可能会提供对 JAC 提供的“语义切入点”的支持——例如,“更改目标对象中实例变量的所有方法”。 |