@Around 建议在 Spring AOP 中究竟是如何工作的? [英] How exactly does an @Around advice work in Spring AOP?

查看:35
本文介绍了@Around 建议在 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

我可以阅读有关周围建议的内容:

I can read this about the AROUND ADVICE:

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.

这是around通知的序列图:

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 点 参数究竟是如何使用的以及它是如何使用的.

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?

我认为在这种情况下,答案是使用抛出后建议,因为当匹配的方法执行通过抛出异常退出时,建议会执行.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆