How exactly does an @Around advice work in Spring AOP?

旧街凉风 提交于 2019-12-21 12:13:23

问题


I am studying Spring AOP module and I have some doubts about how exactly works the AROUND advice.

Reading the official documentation: 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:

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.

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;
}

that perform the implemented chaching behavior before and after that a service method is call. Is it right?

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?

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.

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


回答1:


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;
}

The @Around really has more power and provides more control for end-user to get deal with 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.



来源:https://stackoverflow.com/questions/29193489/how-exactly-does-an-around-advice-work-in-spring-aop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!