java @Around 建议在 Spring AOP 中究竟是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29193489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How exactly does an @Around advice work in Spring AOP?
提问by
I am studying Spring AOPmodule and I have some doubts about how exactly works the AROUNDadvice.
我正在研究Spring AOP模块,但我对AROUND建议的工作原理有一些疑问。
Reading the official documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
阅读官方文档:http: //docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
I can read this about the AROUND ADVICE:
我可以阅读有关周围建议的内容:
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:
这是around通知的序列图:
So from what I can understand I can define an advice (my custom behavior) that will perform before and after a joint pointspecified by a pointcut.
因此,据我所知,我可以定义一个建议(我的自定义行为),该建议将在pointcut指定的关节点之前和之后执行。
So for example I can define an AROUND ADVICEin this way:
例如,我可以通过这种方式定义AROUND ADVICE:
@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;
}
that perform the implemented chaching behavior before and after that a service method is call. Is it right?
在调用服务方法之前和之后执行已实现的 chaching 行为。这样对吗?
The thing that I can't completly understand is how exactly is and how is it used the ProceedingJoinPoint pointparameter.
我无法完全理解的是ProceedingJoinPoint 点参数究竟是如何使用的以及它是如何使用的。
From what I understand it is used to choose if perform or not perform a specific operation but how exactly works?
据我了解,它用于选择是否执行或不执行特定操作,但究竟如何工作?
Another doubt related how correctly use AOP advice is how to respond to the following question:
另一个关于如何正确使用 AOP 建议的疑问是如何回答以下问题:
Which advice do I have to use if I would like to try and catch exceptions?
如果我想尝试捕获异常,我必须使用哪些建议?
I think that in this case the answer is to use the After throwing advicebecause the advice executes when a matched method execution exits by throwing an exception.
我认为在这种情况下,答案是使用After throwing 建议,因为该建议会在匹配的方法执行通过抛出异常退出时执行。
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* ?
但我不确定,因为据我所知,只有在方法抛出异常时才会执行建议。或者也许在这种情况下我必须使用 **AROUND ADVICE* ?
Tnx
田纳西州
回答by Artem Bilan
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:
实际上,所有这些 AOP 注释都作为AbstractAspectJAdvice
. 即使它是@AfterThrowing
,它AspectJAfterThrowingAdvice
也在那里并且无论如何都会被调用:
try {
return mi.proceed();
}
catch (Throwable t) {
if (shouldInvokeOnThrowing(t)) {
invokeAdviceMethod(getJoinPointMatch(), null, t);
}
throw t;
}
The @Around
really has more power and provides more control for end-user to get deal with ProceedingJoinPoint
.
该@Around
真正拥有更多的权力,并为最终用户得到处理更多的控制权ProceedingJoinPoint
。
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
您,但是您可以接触到所有这些建议,尽管您不应该忘记mi.proceed()
从那里打电话。当然,如果需要按照您的逻辑这样做。