此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

Spring 中的 Pointcut API

本节描述了 Spring 如何处理关键的切入点概念。spring-doc.cadn.net.cn

概念

Spring 的切入点模型支持独立于通知类型的切入点重用。您可以 使用相同的切入点定位不同的建议。spring-doc.cadn.net.cn

org.springframework.aop.Pointcutinterface 是中央接口,用于 将 Advice 定位到特定的类和方法。完整的界面如下:spring-doc.cadn.net.cn

public interface Pointcut {

	ClassFilter getClassFilter();

	MethodMatcher getMethodMatcher();
}

拆分Pointcut接口分为两部分,允许重用类和方法 匹配部分和精细组合作(例如执行 “union” 替换为另一个方法 matcher)。spring-doc.cadn.net.cn

ClassFilterinterface 用于将切入点限制为给定的目标集 类。如果matches()method 始终返回 true,则所有目标类都是 匹配。下面的清单显示了ClassFilter接口定义:spring-doc.cadn.net.cn

public interface ClassFilter {

	boolean matches(Class clazz);
}

MethodMatcherinterface 通常更重要。完整的界面如下:spring-doc.cadn.net.cn

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 是为了避免在每次方法调用时都需要进行测试。如果 双参数matchesmethod 返回true对于给定方法,并且isRuntime()method 的 MethodMatcher 返回true,则三个参数 matches 方法是 在每次方法调用时调用。这允许切入点查看传递的参数 添加到紧接 Target 通知开始之前的方法调用。spring-doc.cadn.net.cn

MethodMatcher实现是静态的,这意味着它们的isRuntime()方法 返回false.在这种情况下,三个参数matchesmethod 永远不会被调用。spring-doc.cadn.net.cn

如果可能,请尝试将切入点设为静态,允许 AOP 框架缓存 创建 AOP 代理时的切入点评估结果。

对切入点的作

Spring 支持对切入点进行作(特别是 union 和 intersection)。spring-doc.cadn.net.cn

Union 表示任一切入点匹配的方法。 Intersection 表示两个切入点匹配的方法。 Union 通常更有用。 你可以使用org.springframework.aop.support.Pointcuts类或使用ComposablePointcut类。但是,使用 AspectJ 切入点 表达式通常是一种更简单的方法。spring-doc.cadn.net.cn

AspectJ 表达式切入点

从 2.0 开始,Spring 使用的最重要的切入点类型是org.springframework.aop.aspectj.AspectJExpressionPointcut.这是一个切入点 使用 AspectJ 提供的库来解析 AspectJ 切入点表达式字符串。spring-doc.cadn.net.cn

有关支持的 AspectJ 切入点原语的讨论,请参见上一章spring-doc.cadn.net.cn

便利的切入点实现

Spring 提供了几个方便的切入点实现。您可以使用其中的一些 径直;其他的旨在在特定于应用程序的切入点中被子类化。spring-doc.cadn.net.cn

静态切入点

静态切入点基于方法和目标类,不能考虑 方法的参数。对于大多数用法,静态切入点就足够了,而且是最好的。 Spring 只能在首次调用方法时对静态切入点进行一次求值。 之后,无需在每次方法调用时再次评估切入点。spring-doc.cadn.net.cn

本节的其余部分描述了一些静态切入点实现,这些实现是 包含在 Spring 中。spring-doc.cadn.net.cn

正则表达式切入点

指定静态切入点的一种明显方法是正则表达式。多个 AOP 除了 Spring 之外的框架使这成为可能。org.springframework.aop.support.JdkRegexpMethodPointcut是泛型常规 expression 切入点,该切入点使用 JDK 中的正则表达式支持。spring-doc.cadn.net.cn

使用JdkRegexpMethodPointcut类中,你可以提供模式字符串列表。 如果其中任何一个是匹配项,则切入点的计算结果为true.(因此, 生成的切入点实际上是指定模式的并集。spring-doc.cadn.net.cn

以下示例演示如何使用JdkRegexpMethodPointcut:spring-doc.cadn.net.cn

<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 一起创建,如下例所示:spring-doc.cadn.net.cn

<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-doc.cadn.net.cn

属性驱动的切入点

一种重要的静态切入点类型是元数据驱动的切入点。这将使用 元数据属性的值(通常是源级元数据)。spring-doc.cadn.net.cn

动态切入点

动态切入点的评估成本高于静态切入点。他们考虑了 方法参数以及静态信息。这意味着他们必须 每次方法调用都会进行评估,并且结果不能被缓存,因为参数会 不同。spring-doc.cadn.net.cn

主要示例是control flow切入点。spring-doc.cadn.net.cn

控制流切入点

Spring 控制流切入点在概念上类似于 AspectJcflow切入点 / 虽然威力较弱。(目前无法指定切入点运行 在与另一个切入点匹配的连接点下方。控制流切入点与 当前调用堆栈。例如,如果连接点由方法 在com.mycompany.web软件包或通过SomeCaller类。控制流切入点 通过使用org.springframework.aop.support.ControlFlowPointcut类。spring-doc.cadn.net.cn

控制流切入点在运行时的评估成本甚至比 其他动态切入点。在 Java 1.4 中,成本大约是其他动态的 5 倍 切入点。

切入点超类

Spring 提供了有用的切入点超类来帮助您实现自己的切入点。spring-doc.cadn.net.cn

因为静态切入点最有用,所以你可能应该子类化StaticMethodMatcherPointcut.这只需要实现一个 abstract 方法(尽管您可以覆盖其他方法来自定义行为)。这 以下示例演示如何子类化StaticMethodMatcherPointcut:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

自定义切入点

因为 Spring AOP 中的切入点是 Java 类而不是语言特性(如 AspectJ),你可以声明自定义切入点,无论是静态的还是动态的。习惯 Spring 中的切入点可以是任意复杂的。但是,我们建议使用 AspectJ 切入点 表达式语言(如果可以)。spring-doc.cadn.net.cn

Spring 的更高版本可能会提供对 JAC 提供的“语义切入点”的支持——例如,“更改目标对象中实例变量的所有方法”。