In this article, we will learn the basics of Advice in Spring AOP. if you want to cover the basics of Spring AOP please check these articles, Spring AOP (click_here) and Pointcut Expression (click_here).
Content :
Advice : It's an action taken by Aspect at a particular joinpoint. In application code, it’s a piece of code that executes at a particular join point during application execution. We have different advice and these are represented by different annotations. Let's understand one by one.
-
Before :
- As the name suggests, This type of advice executes before the target method (joinpoint) starts execution.
-
@BeforeAnnotation represents it and this annotation accepts point cut expression. It must be placed on top of methods inside the class annotated with@Aspectonly. - The logic contained by such methods executes before the target joinpoint.
Example :
-
AfterReturning :
- As the name suggests, this executes after the target method (joinpoint) successfully returns a value.
-
It is represented by
@AfterReturningannotation. In below example we used two attributes in this annotation whose names are 'pointcut' and 'returning'. - Here pointcut's value is the pointcut expression and the returning's value is the name of the parameter that the advice method will use as an argument, this argument holds the value returned by the target joinpont.
- In this example, retVal is the name that will refer return value, and the method will use the same to access that return value.
- Also, returning helps to just target that joinpoint which returns the value of type as specified in the advice method argument. Here it's Object. If it is int then it only targets those methods that return an int type of value.
-
AfterThrowing :
- As the name suggests, this executes if the target method (joinpoint) throws an exception.
-
It is represented by
@AfterThrowingannotation. This annotation has two most commonly use attributes whose names are pointcut and throwing. - Pointcut's value is the pointcut expression and the throwing value is the name of the parameter that the advice method will use as an argument, and this argument type is the exception that the target joinpoint can throw. So, using throwing we target only those joinpoints that will throw an exception of type as specified in the advice method argument.
Example :
-
After :
- Executes irrespective of the outcome of the target method (joinpoint), whether it returns some value or throws an exception.
-
@Afterrepresents it.
Example
Whatever advice we discussed so far, can also take the first argument of type JoinPoint, which is optional. For Example, In the case of AfterThrowing advice if we are using JoinPoint, then it will look like as below.
JoinPoint is an interface, it represents the point during the execution of the program where advice is being applied. It provides metadata about the target method being executed, its argument, its name, etc.Some mostly use methods are below,
joinPoint.getSignuature() |
return method signature, this include method name, return type and parameter type. |
joinPoint.getArgs() |
return an array of argument passed to the method. |
joinPoint.getTarget() |
returns target object. |
joinPoint.getKind() |
returns the kind of joinpoint. |
joinPoint.getThrowable() |
access thrown exception by target method. |
Example :
Example :
Example :
Around :
- It executes around the target method (joinpoint), so far whatever advice we saw, they either execute after or before, but not both ways, using around advice we can execute advice before and after target joinpoint.
-
It is defined using
@Aroundannotation. -
In the case of
@Around, the advice method should return the type of Object to make it general or else whatever return type is provided byproceed()method as a result, we can also define void but in that case, it will return null to a caller who calls the target method. -
proceed()method ofProceedingJoinPointcan throw aThrowableexception, so we need to handle it in the advice method signature by addingthrows Throwable. -
the first argument must of type
ProceedingJoinPoint. This method must use theproceed()method ofProceedingJoinPointto continue the execution of the target joinpoint.-
Here
ProceedingJoinPointis an interface that extends theJoinPointinterface, so it has all functionalities whateverJoinPointprovides along with additional functionalities. - It is mainly designed to be used with Around advice.
-
Here
-
Using the
proceed()method we can continue the execution of the target method, skip it, and modify the return value. -
Important point, it's not possible to use only
JoinPointin place ofProceedingJoinPointwith Around advice. But we can use these together, the first argument must beProceedingJoinPointand the second argument can beJoinPoint(Optional).-
In case you forget to pass
ProceedingJoinPointas an argument in the advice method annotated with@Around, then the target method invocation will skip, and the caller method will always get null.
Example
org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public int com.coderstuff01.aopdemo.service.MyServiceImpl.getSum(int,int)] -
In case you forget to pass










Comments
Post a Comment