基于 Schema 的 AOP 支持

如果您更喜欢基于 XML 的格式,Spring 也支持使用 aop 命名空间标签定义切面。支持与使用 @AspectJ 样式完全相同的切入点表达式和通知类型。因此,在本节中,我们重点介绍这种语法,并请读者参考上一节(@AspectJ 支持)的讨论,以了解如何编写切入点表达式和通知参数的绑定。

要使用本节中描述的 aop 命名空间标签,您需要导入 spring-aop schema,如 基于 XML Schema 的配置 中所述。有关如何在 aop 命名空间中导入标签的信息,请参阅 AOP schema

在您的 Spring 配置中,所有切面和顾问元素都必须放在 <aop:config> 元素中(您可以在一个应用程序上下文配置中拥有多个 <aop:config> 元素)。<aop:config> 元素可以包含切入点、顾问和切面元素(请注意,这些元素必须按此顺序声明)。

<aop:config> 样式配置大量使用了 Spring 的 自动代理 机制。如果您已经通过使用 BeanNameAutoProxyCreator 或类似方式显式使用了自动代理,这可能会导致问题(例如通知未织入)。推荐的使用模式是只使用 <aop:config> 样式或只使用 AutoProxyCreator 样式,切勿混用。

声明切面

当您使用 schema 支持时,切面是一个常规的 Java 对象,在您的 Spring 应用程序上下文中定义为一个 bean。状态和行为捕获在对象的字段和方法中,而切入点和通知信息则捕获在 XML 中。

您可以使用 <aop:aspect> 元素声明切面,并通过使用 ref 属性引用支持 bean,如下例所示

<aop:config>
	<aop:aspect id="myAspect" ref="aBean">
		...
	</aop:aspect>
</aop:config>

<bean id="aBean" class="...">
	...
</bean>

支持切面(本例中为 aBean)的 bean 当然可以像任何其他 Spring bean 一样进行配置和依赖注入。

声明切入点

您可以在 <aop:config> 元素中声明一个_命名切入点_,从而允许在多个切面和顾问之间共享切入点定义。

表示服务层中任何业务服务执行的切入点可以定义如下

<aop:config>

	<aop:pointcut id="businessService"
		expression="execution(* com.xyz.service.*.*(..))" />

</aop:config>

请注意,切入点表达式本身使用与 @AspectJ 支持 中描述的相同的 AspectJ 切入点表达式语言。如果使用基于 schema 的声明样式,您还可以引用 @Aspect 类型中定义的_命名切入点_,在切入点表达式中。因此,定义上述切入点的另一种方法如下所示

<aop:config>

	<aop:pointcut id="businessService"
		expression="com.xyz.CommonPointcuts.businessService()" /> (1)

</aop:config>
1 引用 共享命名切入点定义 中定义的 businessService 命名切入点。

在切面_内部_声明切入点与声明顶级切入点非常相似,如下例所示

<aop:config>

	<aop:aspect id="myAspect" ref="aBean">

		<aop:pointcut id="businessService"
			expression="execution(* com.xyz.service.*.*(..))"/>

		...
	</aop:aspect>

</aop:config>

与 @AspectJ 切面非常相似,通过基于 schema 的定义样式声明的切入点可以收集连接点上下文。例如,以下切入点将 this 对象作为连接点上下文并将其传递给通知

<aop:config>

	<aop:aspect id="myAspect" ref="aBean">

		<aop:pointcut id="businessService"
			expression="execution(* com.xyz.service.*.*(..)) &amp;&amp; this(service)"/>

		<aop:before pointcut-ref="businessService" method="monitor"/>

		...
	</aop:aspect>

</aop:config>

通知必须通过包含具有匹配名称的参数来声明以接收收集到的连接点上下文,如下所示

  • Java

  • Kotlin

public void monitor(Object service) {
	// ...
}
fun monitor(service: Any) {
	// ...
}

当组合切入点子表达式时,&amp;&amp; 在 XML 文档中很不方便,因此您可以使用 andornot 关键字分别代替 &amp;&amp;||!。例如,前面的切入点可以更好地写成如下

<aop:config>

	<aop:aspect id="myAspect" ref="aBean">

		<aop:pointcut id="businessService"
			expression="execution(* com.xyz.service.*.*(..)) and this(service)"/>

		<aop:before pointcut-ref="businessService" method="monitor"/>

		...
	</aop:aspect>

</aop:config>

请注意,以这种方式定义的切入点通过其 XML id 引用,不能用作命名切入点来形成复合切入点。因此,基于 schema 的定义样式中的命名切入点支持比 @AspectJ 样式提供的更为有限。

声明通知

基于 schema 的 AOP 支持使用与 @AspectJ 样式相同的五种通知,并且它们具有完全相同的语义。

前置通知

前置通知在匹配方法执行之前运行。它在 <aop:aspect> 中通过使用 <aop:before> 元素声明,如下例所示

<aop:aspect id="beforeExample" ref="aBean">

	<aop:before
		pointcut-ref="dataAccessOperation"
		method="doAccessCheck"/>

	...

</aop:aspect>

在上面的例子中,dataAccessOperation 是在顶层(<aop:config>)定义的_命名切入点_的 id(参见 声明切入点)。

正如我们在讨论 @AspectJ 样式时所指出的,使用_命名切入点_可以显著提高代码的可读性。有关详细信息,请参阅 共享命名切入点定义

要以内联方式定义切入点,请将 pointcut-ref 属性替换为 pointcut 属性,如下所示

<aop:aspect id="beforeExample" ref="aBean">

	<aop:before
		pointcut="execution(* com.xyz.dao.*.*(..))"
		method="doAccessCheck"/>

	...

</aop:aspect>

method 属性标识提供通知主体的方法(doAccessCheck)。此方法必须为包含通知的切面元素引用的 bean 定义。在执行数据访问操作(与切入点表达式匹配的方法执行连接点)之前,将调用切面 bean 上的 doAccessCheck 方法。

后置返回通知

后置返回通知在匹配方法执行正常完成时运行。它以与前置通知相同的方式在 <aop:aspect> 中声明。以下示例显示了如何声明它

<aop:aspect id="afterReturningExample" ref="aBean">

	<aop:after-returning
		pointcut="execution(* com.xyz.dao.*.*(..))"
		method="doAccessCheck"/>

	...
</aop:aspect>

与 @AspectJ 样式一样,您可以在通知主体中获取返回值。为此,请使用 returning 属性指定应将返回值传递到的参数名称,如下例所示

<aop:aspect id="afterReturningExample" ref="aBean">

	<aop:after-returning
		pointcut="execution(* com.xyz.dao.*.*(..))"
		returning="retVal"
		method="doAccessCheck"/>

	...
</aop:aspect>

doAccessCheck 方法必须声明一个名为 retVal 的参数。此参数的类型以与 @AfterReturning 描述的相同方式约束匹配。例如,您可以将方法签名声明如下

  • Java

  • Kotlin

public void doAccessCheck(Object retVal) {...
fun doAccessCheck(retVal: Any) {...

后置异常通知

后置异常通知在匹配方法执行因抛出异常而退出时运行。它在 <aop:aspect> 中通过使用 after-throwing 元素声明,如下例所示

<aop:aspect id="afterThrowingExample" ref="aBean">

	<aop:after-throwing
		pointcut="execution(* com.xyz.dao.*.*(..))"
		method="doRecoveryActions"/>

	...
</aop:aspect>

与 @AspectJ 样式一样,您可以在通知主体中获取抛出的异常。为此,请使用 throwing 属性指定应将异常传递到的参数名称,如下例所示

<aop:aspect id="afterThrowingExample" ref="aBean">

	<aop:after-throwing
		pointcut="execution(* com.xyz.dao.*.*(..))"
		throwing="dataAccessEx"
		method="doRecoveryActions"/>

	...
</aop:aspect>

doRecoveryActions 方法必须声明一个名为 dataAccessEx 的参数。此参数的类型以与 @AfterThrowing 描述的相同方式约束匹配。例如,方法签名可以声明如下

  • Java

  • Kotlin

public void doRecoveryActions(DataAccessException dataAccessEx) {...
fun doRecoveryActions(dataAccessEx: DataAccessException) {...

后置(最终)通知

后置(最终)通知无论匹配方法执行如何退出都会运行。您可以通过使用 after 元素来声明它,如下例所示

<aop:aspect id="afterFinallyExample" ref="aBean">

	<aop:after
		pointcut="execution(* com.xyz.dao.*.*(..))"
		method="doReleaseLock"/>

	...
</aop:aspect>

环绕通知

最后一种通知是_环绕_通知。环绕通知在匹配方法的执行“周围”运行。它有机会在方法运行之前和之后进行工作,并确定方法何时、如何以及是否实际运行。如果您需要以线程安全的方式在方法执行之前和之后共享状态(例如,启动和停止计时器),通常会使用环绕通知。

始终使用满足您要求的功能最弱的通知形式。

例如,如果前置通知足以满足您的需求,请不要使用_环绕_通知。

您可以使用 aop:around 元素声明环绕通知。通知方法应将 Object 声明为其返回类型,并且方法的第一个参数必须是 ProceedingJoinPoint 类型。在通知方法的主体中,您必须在 ProceedingJoinPoint 上调用 proceed(),以便基础方法运行。调用不带参数的 proceed() 将导致在调用基础方法时向其提供调用者的原始参数。对于高级用例,proceed() 方法有一个重载变体,它接受一个参数数组(Object[])。数组中的值将在调用基础方法时用作其参数。有关使用 Object[] 调用 proceed 的注意事项,请参阅 环绕通知

以下示例显示了如何在 XML 中声明环绕通知

<aop:aspect id="aroundExample" ref="aBean">

	<aop:around
		pointcut="execution(* com.xyz.service.*.*(..))"
		method="doBasicProfiling"/>

	...
</aop:aspect>

doBasicProfiling 通知器的实现可以与 @AspectJ 示例完全相同(当然,减去注解),如下例所示

  • Java

  • Kotlin

public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	// start stopwatch
	Object retVal = pjp.proceed();
	// stop stopwatch
	return retVal;
}
fun doBasicProfiling(pjp: ProceedingJoinPoint): Any? {
	// start stopwatch
	val retVal = pjp.proceed()
	// stop stopwatch
	return pjp.proceed()
}

通知参数

基于 schema 的声明样式支持与 @AspectJ 支持相同的方式进行完全类型化的通知——通过按名称匹配切入点参数与通知方法参数。有关详细信息,请参阅 通知参数。如果您希望显式指定通知方法的参数名称(不依赖前面描述的检测策略),您可以通过使用通知元素的 arg-names 属性来实现,该属性以与通知注解中的 argNames 属性相同的方式处理(如 确定参数名称 中所述)。以下示例显示了如何在 XML 中指定参数名称

<aop:before
	pointcut="com.xyz.Pointcuts.publicMethod() and @annotation(auditable)" (1)
	method="audit"
	arg-names="auditable" />
1 引用 组合切入点表达式 中定义的 publicMethod 命名切入点。

arg-names 属性接受逗号分隔的参数名称列表。

以下是基于 XSD 方法的一个稍微复杂一点的示例,它展示了一些与多个强类型参数结合使用的环绕通知

  • Java

  • Kotlin

package com.xyz.service;

public interface PersonService {

	Person getPerson(String personName, int age);
}

public class DefaultPersonService implements PersonService {

	public Person getPerson(String name, int age) {
		return new Person(name, age);
	}
}
package com.xyz.service

interface PersonService {

	fun getPerson(personName: String, age: Int): Person
}

class DefaultPersonService : PersonService {

	fun getPerson(name: String, age: Int): Person {
		return Person(name, age)
	}
}

接下来是切面。请注意 profile(..) 方法接受多个强类型参数的事实,其中第一个参数恰好是用于继续方法调用的连接点。此参数的存在表明 profile(..) 将用作 around 通知,如下例所示

  • Java

  • Kotlin

package com.xyz;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class SimpleProfiler {

	public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
		StopWatch clock = new StopWatch("Profiling for '" + name + "' and '" + age + "'");
		try {
			clock.start(call.toShortString());
			return call.proceed();
		} finally {
			clock.stop();
			System.out.println(clock.prettyPrint());
		}
	}
}
package com.xyz

import org.aspectj.lang.ProceedingJoinPoint
import org.springframework.util.StopWatch

class SimpleProfiler {

	fun profile(call: ProceedingJoinPoint, name: String, age: Int): Any? {
		val clock = StopWatch("Profiling for '$name' and '$age'")
		try {
			clock.start(call.toShortString())
			return call.proceed()
		} finally {
			clock.stop()
			println(clock.prettyPrint())
		}
	}
}

最后,以下 XML 配置示例实现了特定连接点的前述通知的执行

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- this is the object that will be proxied by Spring's AOP infrastructure -->
	<bean id="personService" class="com.xyz.service.DefaultPersonService"/>

	<!-- this is the actual advice itself -->
	<bean id="profiler" class="com.xyz.SimpleProfiler"/>

	<aop:config>
		<aop:aspect ref="profiler">

			<aop:pointcut id="theExecutionOfSomePersonServiceMethod"
				expression="execution(* com.xyz.service.PersonService.getPerson(String,int))
				and args(name, age)"/>

			<aop:around pointcut-ref="theExecutionOfSomePersonServiceMethod"
				method="profile"/>

		</aop:aspect>
	</aop:config>

</beans>

考虑以下驱动脚本

  • Java

  • Kotlin

public class Boot {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService person = ctx.getBean(PersonService.class);
		person.getPerson("Pengo", 12);
	}
}
fun main() {
	val ctx = ClassPathXmlApplicationContext("beans.xml")
	val person = ctx.getBean(PersonService.class)
	person.getPerson("Pengo", 12)
}

有了这样的 Boot 类,我们将在标准输出上得到类似于以下的输出

StopWatch 'Profiling for 'Pengo' and '12': running time (millis) = 0
-----------------------------------------
ms     %     Task name
-----------------------------------------
00000  ?  execution(getFoo)

通知排序

当多个通知需要在同一个连接点(执行方法)上运行时,排序规则如 通知排序 中所述。切面之间的优先级通过 <aop:aspect> 元素中的 order 属性或通过向支持切面的 bean 添加 @Order 注解或通过让 bean 实现 Ordered 接口来确定。

与在同一个 @Aspect 类中定义的通知方法的优先级规则相反,当在同一个 <aop:aspect> 元素中定义的两个通知都需要在同一个连接点上运行时,优先级由通知元素在封闭的 <aop:aspect> 元素中声明的顺序决定,从最高优先级到最低优先级。

例如,给定一个在同一个 <aop:aspect> 元素中定义的 around 通知和一个 before 通知都适用于同一个连接点,为了确保 around 通知具有比 before 通知更高的优先级,<aop:around> 元素必须在 <aop:before> 元素之前声明。

根据经验法则,如果您发现在同一个 <aop:aspect> 元素中定义了多个适用于同一个连接点的通知,请考虑将这些通知方法合并为每个 <aop:aspect> 元素中每个连接点一个通知方法,或者将通知分解为可以在切面级别排序的独立 <aop:aspect> 元素。

引入

引入(在 AspectJ 中称为交叉类型声明)允许切面声明被通知对象实现给定接口,并代表这些对象提供该接口的实现。

您可以使用 aop:declare-parents 元素在 aop:aspect 中进行引入。您可以使用 aop:declare-parents 元素声明匹配类型具有新的父级(因此得名)。例如,给定一个名为 UsageTracked 的接口和一个名为 DefaultUsageTracked 的该接口的实现,以下切面声明所有服务接口的实现者也实现 UsageTracked 接口。(例如,为了通过 JMX 公开统计信息。)

<aop:aspect id="usageTrackerAspect" ref="usageTracking">

	<aop:declare-parents
		types-matching="com.xyz.service.*+"
		implement-interface="com.xyz.service.tracking.UsageTracked"
		default-impl="com.xyz.service.tracking.DefaultUsageTracked"/>

	<aop:before
		pointcut="execution(* com.xyz..service.*.*(..))
			and this(usageTracked)"
			method="recordUsage"/>

</aop:aspect>

支持 usageTracking bean 的类将包含以下方法

  • Java

  • Kotlin

public void recordUsage(UsageTracked usageTracked) {
	usageTracked.incrementUseCount();
}
fun recordUsage(usageTracked: UsageTracked) {
	usageTracked.incrementUseCount()
}

要实现的接口由 implement-interface 属性确定。types-matching 属性的值是一个 AspectJ 类型模式。任何匹配类型的 bean 都实现 UsageTracked 接口。请注意,在前面示例的前置通知中,服务 bean 可以直接用作 UsageTracked 接口的实现。要以编程方式访问 bean,您可以编写如下代码

  • Java

  • Kotlin

UsageTracked usageTracked = context.getBean("myService", UsageTracked.class);
val usageTracked = context.getBean("myService", UsageTracked.class)

切面实例化模型

schema 定义的切面唯一支持的实例化模型是单例模型。其他实例化模型可能会在未来的版本中支持。

顾问

“顾问”的概念来自 Spring 中定义的 AOP 支持,在 AspectJ 中没有直接的等价物。顾问就像一个小的、自包含的切面,它只有一段通知。通知本身由一个 bean 表示,并且必须实现 Spring 中的通知类型 中描述的一个通知接口。顾问可以利用 AspectJ 切入点表达式。

Spring 通过 <aop:advisor> 元素支持顾问概念。您最常看到它与事务通知一起使用,事务通知在 Spring 中也有自己的命名空间支持。以下示例显示了一个顾问

<aop:config>

	<aop:pointcut id="businessService"
		expression="execution(* com.xyz.service.*.*(..))"/>

	<aop:advisor
		pointcut-ref="businessService"
		advice-ref="tx-advice" />

</aop:config>

<tx:advice id="tx-advice">
	<tx:attributes>
		<tx:method name="*" propagation="REQUIRED"/>
	</tx:attributes>
</tx:advice>

除了前面示例中使用的 pointcut-ref 属性,您还可以使用 pointcut 属性以内联方式定义切入点表达式。

要定义顾问的优先级,以便通知可以参与排序,请使用 order 属性定义顾问的 Ordered 值。

AOP Schema 示例

本节展示了 AOP 示例 中的并发锁定失败重试示例在重写为使用 schema 支持时的样子。

业务服务的执行有时会因并发问题(例如,死锁失败者)而失败。如果操作重试,很可能在下次尝试时成功。对于在这种情况下适合重试的业务服务(幂等操作不需要返回给用户进行冲突解决),我们希望透明地重试操作,以避免客户端看到 PessimisticLockingFailureException。这是一个明显跨越服务层中多个服务的要求,因此非常适合通过切面实现。

因为我们想要重试操作,所以我们需要使用环绕通知,以便我们可以多次调用 proceed。以下清单显示了基本的切面实现(它是一个使用 schema 支持的常规 Java 类)

  • Java

  • Kotlin

public class ConcurrentOperationExecutor implements Ordered {

	private static final int DEFAULT_MAX_RETRIES = 2;

	private int maxRetries = DEFAULT_MAX_RETRIES;
	private int order = 1;

	public void setMaxRetries(int maxRetries) {
		this.maxRetries = maxRetries;
	}

	public int getOrder() {
		return this.order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
		int numAttempts = 0;
		PessimisticLockingFailureException lockFailureException;
		do {
			numAttempts++;
			try {
				return pjp.proceed();
			}
			catch(PessimisticLockingFailureException ex) {
				lockFailureException = ex;
			}
		} while(numAttempts <= this.maxRetries);
		throw lockFailureException;
	}
}
class ConcurrentOperationExecutor : Ordered {

	private val DEFAULT_MAX_RETRIES = 2

	private var maxRetries = DEFAULT_MAX_RETRIES
	private var order = 1

	fun setMaxRetries(maxRetries: Int) {
		this.maxRetries = maxRetries
	}

	override fun getOrder(): Int {
		return this.order
	}

	fun setOrder(order: Int) {
		this.order = order
	}

	fun doConcurrentOperation(pjp: ProceedingJoinPoint): Any? {
		var numAttempts = 0
		var lockFailureException: PessimisticLockingFailureException
		do {
			numAttempts++
			try {
				return pjp.proceed()
			} catch (ex: PessimisticLockingFailureException) {
				lockFailureException = ex
			}

		} while (numAttempts <= this.maxRetries)
		throw lockFailureException
	}
}

请注意,切面实现了 Ordered 接口,这样我们就可以将切面的优先级设置高于事务通知(我们希望每次重试都有一个新的事务)。maxRetriesorder 属性都由 Spring 配置。主要操作发生在 doConcurrentOperation 环绕通知方法中。我们尝试继续。如果我们因 PessimisticLockingFailureException 而失败,我们会再次尝试,除非我们已用尽所有重试尝试。

此类的代码与 @AspectJ 示例中使用的代码相同,但删除了注解。

相应的 Spring 配置如下

<aop:config>

	<aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">

		<aop:pointcut id="idempotentOperation"
			expression="execution(* com.xyz.service.*.*(..))"/>

		<aop:around
			pointcut-ref="idempotentOperation"
			method="doConcurrentOperation"/>

	</aop:aspect>

</aop:config>

<bean id="concurrentOperationExecutor"
	class="com.xyz.service.impl.ConcurrentOperationExecutor">
		<property name="maxRetries" value="3"/>
		<property name="order" value="100"/>
</bean>

请注意,目前我们假设所有业务服务都是幂等的。如果不是这种情况,我们可以细化切面,使其仅重试真正幂等的操作,通过引入 Idempotent 注解并使用该注解来注解服务操作的实现,如下例所示

  • Java

  • Kotlin

@Retention(RetentionPolicy.RUNTIME)
// marker annotation
public @interface Idempotent {
}
@Retention(AnnotationRetention.RUNTIME)
// marker annotation
annotation class Idempotent

为了使切面仅重试幂等操作而进行的更改涉及到细化切入点表达式,使其仅匹配 @Idempotent 操作,如下所示

<aop:pointcut id="idempotentOperation"
		expression="execution(* com.xyz.service.*.*(..)) and
		@annotation(com.xyz.service.Idempotent)"/>
© . This site is unofficial and not affiliated with VMware.