8
votes

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:

enter image description here

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
While it is surely showing good intentions to "study" Spring AOP, your writing elaborate theoretical treatises will not get you very far in understanding the topic. My recommendation is to actually write code and just give your theories a try in order to find out whether they are right or wrong. Your question is so elaborate, in the meantime you could have found out by actually trying.kriegaex

1 Answers

5
votes

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.