Advice is the implementation of the cross-cutting functionality. An Adivce provides the code for implementation of the service.
There are four types of Advice:
Before Advice
After Advice
Around Advice
Throws Advice
After Advice is same as Before Advice. But in order to create our after advice in spring, our class should implement an interface called AfterReturningAdvice, this interface is given in org.springframework.aop.* package. And we need to override a method given by this interface called afterReturning().
The returnValuef is the return value of the logic method, we can use it to access the return of the logic function. The method parameter is the logic method, we can use getName() to access the name of method. And the args parameter is arguments of logic method.
Let’s see full example.
1 2 3 4 5 6 7 8
package spring.test.aop;
publicinterfaceMyLogicInterface { intadd(int a, int b); intsubtract(int a, int b); }
In this interface, we changed the method’s signature, we have return value for both two method.