@Around建议究竟如何在Spring AOP中运行? [英] How exactly does an @Around advice work in Spring AOP?
问题描述
我正在学习 Spring AOP 模块,我对 AROUND 建议的确切运作方式有所怀疑。
I am studying Spring AOP module and I have some doubts about how exactly works the AROUND advice.
阅读官方文档: http:// docs .spring.io / spring / docs / current / spring-framework-reference / html / aop.html
我可以读到这个关于的信息一般建议:
围绕建议:围绕连接点的建议,例如方法
调用。这是最有力的建议。周围的建议
可以在方法调用之前和之后执行自定义行为。
还负责选择是继续加入点
还是通过返回自己的
返回值或抛出异常来快速建议的方法执行。
Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
这是周围建议的序列图:
And this is the sequence diagram of the around advice:
所以从我能理解的内容我可以定义一个建议(我的自定义行为)将在切入点指定的关节点之前和之后执行。
So from what I can understand I can define an advice (my custom behavior) that will perform before and after a joint point specified by a pointcut.
所以例如我可以用这种方式定义 AROUND ADVICE :
So for example I can define an AROUND ADVICE in this way:
@Around("execution(@example.Cacheable * rewards.service..*.*(..))")
public Object cache(ProceedingJoinPoint point) throws Throwable {
Object value = cacheStore.get(cacheKey(point));
if (value == null) {
value = point.proceed();
cacheStore.put(cacheKey(point), value);
}
return value;
}
执行调用服务方法之前和之后执行的chaching行为。是不是?
that perform the implemented chaching behavior before and after that a service method is call. Is it right?
我无法完全理解的是它是如何使用 ProceedingJoinPoint point 参数。
The thing that I can't completly understand is how exactly is and how is it used the ProceedingJoinPoint point parameter.
根据我的理解,它用于选择执行或不执行特定操作但具体如何工作?
From what I understand it is used to choose if perform or not perform a specific operation but how exactly works?
另一个疑问是如何正确使用AOP建议如何回答以下问题:
Another doubt related how correctly use AOP advice is how to respond to the following question:
如果我愿意,我必须使用哪些建议喜欢尝试捕捉
例外?
Which advice do I have to use if I would like to try and catch exceptions?
我认为在这种情况下答案是使用 After抛出建议因为当匹配的方法执行通过抛出异常而退出时执行通知。
I think that in this case the answer is to use the After throwing advice because the advice executes when a matched method execution exits by throwing an exception.
但我不确定,因为据我了解的建议仅当方法抛出异常时才执行。或者在这种情况下我必须使用** AROUND ADVICE *?
But I am not sure about it because from what I understand the advice is executed only if a method throws an exception. Or maybe in this case I have to use the **AROUND ADVICE* ?
Tnx
推荐答案
实际上,所有这些AOP注释都是作为 AbstractAspectJAdvice
的具体实现公开的。即使它是 @AfterThrowing
,它的 AspectJAfterThrowingAdvice
仍然存在并且仍然被调用:
Actually all those AOP annotations are exposed as concrete implementation of AbstractAspectJAdvice
. And even if it is @AfterThrowing
, its AspectJAfterThrowingAdvice
is there and invoked anyway:
try {
return mi.proceed();
}
catch (Throwable t) {
if (shouldInvokeOnThrowing(t)) {
invokeAdviceMethod(getJoinPointMatch(), null, t);
}
throw t;
}
@Around
真的有更多的权力,并为最终用户提供更多的控制权来处理 ProceedingJoinPoint
。
The @Around
really has more power and provides more control for end-user to get deal with ProceedingJoinPoint
.
这取决于研究所有这些建议类型,但是使用 @Around
你可以到达所有这些,但你不应该忘记打电话给 mi.proceed()
从那里。当然,如果你的逻辑需要这样做。
It's up to to study all those advice type, but with @Around
you can reach all of them, although you shouldn't forget to call mi.proceed()
from there. If need to do that by your logic, of course.
这篇关于@Around建议究竟如何在Spring AOP中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!