对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
基于模式的 AOP 支持
如果您更喜欢基于 XML 的格式,Spring 还提供了对定义方面的支持
使用aop
namespace 标签。完全相同的切入点表达式和建议类型
与使用@AspectJ样式一样,支持。因此,在本节中,我们将重点关注
该语法,并让读者参考上一节中的讨论
(@AspectJ支持)用于理解编写切入点表达式和绑定
的建议参数。
要使用本节中描述的 aop 命名空间标签,您需要导入spring-aop
schema,如 XML Schema-based configuration 中所述。请参阅AOP架构,了解如何导入aop
Namespace。
在 Spring 配置中,所有方面和顾问元素都必须放置在
一<aop:config>
元素(可以有多个<aop:config>
元素中的
应用程序上下文配置)。一<aop:config>
元素可以包含切入点,
advisor 和 aspect 元素(请注意,这些元素必须按该顺序声明)。
这<aop:config> 配置样式大量使用了 Spring 的自动代理机制。这可能会导致问题(例如建议
not being woven),如果你已经通过使用BeanNameAutoProxyCreator 或类似的东西。推荐的使用模式是
仅使用<aop:config> style 或仅AutoProxyCreator style 和
永远不要混合它们。 |
声明方面
当您使用模式支持时,方面是定义为 bean 的常规 Java 对象 您的 Spring 应用程序上下文。状态和行为在字段中捕获,并且 对象的方法,切入点和通知信息被捕获在 XML 中。
您可以使用<aop:aspect>
元素,并引用后备 bean
通过使用ref
属性,如以下示例所示:
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
...
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
支持方面 (aBean
在这种情况下)当然可以配置和
像任何其他 Spring Bean 一样注入依赖。
声明切入点
您可以在<aop:config>
元素,让切入点
定义在多个方面和顾问之间共享。
表示服务层中任何业务服务执行的切入点可以 定义如下:
<aop:config>
<aop:pointcut id="businessService"
expression="execution(* com.xyz.service.*.*(..))" />
</aop:config>
请注意,切入点表达式本身使用相同的 AspectJ 切入点表达式
@AspectJ支持中所述的语言。如果使用基于架构的声明
style,也可以参考在@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方面大致相同,使用基于模式的
定义样式可以收集连接点上下文。例如,以下切入点
收集this
object 作为连接点上下文并将其传递给通知:
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id="businessService"
expression="execution(* com.xyz.service.*.*(..)) && this(service)"/>
<aop:before pointcut-ref="businessService" method="monitor"/>
...
</aop:aspect>
</aop:config>
必须声明通知以接收收集的连接点上下文,方法是包含 参数,如下所示:
-
Java
-
Kotlin
public void monitor(Object service) {
// ...
}
fun monitor(service: Any) {
// ...
}
组合切入点子表达式时,&&
在 XML 中很尴尬
文档,因此您可以使用and
,or
和not
关键字代替&&
,||
和!
分别。例如,前面的切入点可以更好地写成
遵循:
<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
并且不能是
用作命名点切入以形成复合切入点。中的命名切入点支座
因此,基于模式的定义风格比@AspectJ提供的定义风格更受限制
风格。
声明建议
基于模式的 AOP 支持使用与 @AspectJ 样式相同的五种建议,并且它们具有 完全相同的语义。
建议前
before 通知在匹配的方法执行之前运行。它是在<aop:aspect>
通过使用<aop:before>
元素,如以下示例所示:
<aop:aspect id="beforeExample" ref="aBean">
<aop:before
pointcut-ref="dataAccessOperation"
method="doAccessCheck"/>
...
</aop:aspect>
在上面的示例中,dataAccessOperation
是id
定义在顶部 (<aop:config>
)级别(请参阅声明切入点)。
正如我们在讨论@AspectJ样式时所指出的,使用命名切入点可以显着提高代码的可读性。请参阅共享命名切入点定义 详。 |
要改为内联定义切入点,请将pointcut-ref
属性,并带有pointcut
属性,如下所示:
<aop:aspect id="beforeExample" ref="aBean">
<aop:before
pointcut="execution(* com.xyz.dao.*.*(..))"
method="doAccessCheck"/>
...
</aop:aspect>
这method
属性标识方法(doAccessCheck
) 提供建议。必须为方面元素引用的 bean 定义此方法包含建议。在执行数据访问作之前(方法执行与切入点表达式匹配的连接点),该doAccessCheck
方法在方面bean 被调用。
退货后建议
返回通知后,当匹配的方法执行正常完成时运行。 是的 在<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>
周边建议
最后一种建议是围绕建议。围绕建议“围绕”匹配的方法的执行。它有机会在方法之前和之后做工作并确定何时、如何运行,甚至该方法是否真的开始运行。如果您需要在方法之前和之后共享状态,则通常使用 Around 建议以线程安全的方式执行——例如,启动和停止计时器。
始终使用满足您要求的最不强大的建议形式。 例如,如果之前的建议足以满足您的需求,请不要使用周围建议。 |
您可以使用aop:around
元素。建议方法应
宣Object
作为其返回类型,并且该方法的第一个参数必须是
类型ProceedingJoinPoint
.在通知方法的正文中,您必须调用proceed()
在ProceedingJoinPoint
以便运行底层方法。
调用proceed()
没有参数将导致调用者的原始参数
在调用基础方法时提供给它。对于高级用例,有
是proceed()
接受参数数组的方法
(Object[]
).数组中的值将用作基础
方法。有关通话的注意事项,请参阅 Around Adviceproceed
使用Object[]
.
以下示例显示了如何在 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()
}
通知参数
基于模式的声明样式支持完全类型的通知,其方式与
描述@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)
建议订购
当需要在同一连接点运行多个通知时(执行方法)
排序规则如通知排序中所述。优先顺序
between aspect 是通过order
属性中的<aop:aspect>
元素或
通过添加@Order
对支持该方面的 bean 进行注释,或者通过
bean 实现了Ordered
接口。
与同一 例如,给定一个 作为一般经验法则,如果您发现您定义了多条建议在同一个 |
介绍
介绍(在 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
接口。 请注意,在前面示例的前面建议中,Service Bean 可以直接用作 这UsageTracked
接口。要以编程方式访问 bean,您可以编写
以后:
-
Java
-
Kotlin
UsageTracked usageTracked = context.getBean("myService", UsageTracked.class);
val usageTracked = context.getBean("myService", UsageTracked.class)
顾问
“顾问”的概念来自 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 架构示例
本节显示 AOP 示例中的并发锁定失败重试示例在使用架构支持重写时的外观。
业务服务的执行有时会由于并发问题而失败(对于
例如,死锁失败者)。如果重试作,则很可能会成功
在下一次尝试中。对于适合重试的业务服务
条件(冪等作,不需要返回给用户进行冲突
resolution),我们希望透明地重试作,以避免客户端看到PessimisticLockingFailureException
.这是一个明显跨越的要求
服务层中的多个服务,因此非常适合通过
方面。
因为我们想重试作,所以我们需要使用 around 建议,以便我们可以
叫proceed
多次。以下列表显示了基本方面实现
(这是一个使用模式支持的常规 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
接口,以便我们可以设置
高于交易建议的方面(我们每次都希望有新的交易
重试)。这maxRetries
和order
属性都由 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)"/>