Can we say the @Around annotation in spring boot AOP is a combination of @Before and @After method.
@Around("myPointCut()")
public Object applicationLogger(ProceedingJoinPoint joinPoint){
logger.info(" for the Method : " + joinPoint.getSignature().getName() +
" Request received : {}", Arrays.toString(joinPoint.getArgs()));
Object res = null;
try {
res = joinPoint.proceed();
logger.info(" for the Method :"+ joinPoint.getSignature().getName() +
" Response received : {}", res);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return res;
}
With this advice method annotated with @Around annotation, we can get the input parameters and also the returned object with proceed() which creates a proxy demo class and calls the actual implemented method. If let's say we don't have @Around annotation, then we would have to use the @Before and @After annotations and two separate methods to achieve the input and output parameters.
So can we say just @Around annotation is enough and we don't require @Before, @After, @AfterReturning annotations as these are all catered by @Around.
You are right. Basically for a method that has multiple @Before , @After , @AfterReturing and @AfterThrowing advices applied on it can be converted to a single @Around advice.
The main difference is that @Around is a more generic advice type which gives you the most flexibility. You have total control on how to apply advices on a method invocation while the other more specific advice types provide you a declarative approach for doing such thing (e.g. use @Order to define execution order). So it is just a matter if you like to do it in a imperative (i.e @Around) or declarative way.
And the spring documentation also has some comments about how to choose between them. I modify them slightly based on the context of your question as follows :
@Aroundoffer interoperability with other AOP Alliance-compliant AOP implementations. The other more specific advice type implement common AOP concepts but in a Spring-specific way. While there is an advantage in using the most specific advice type, stick with@Aroundadvice if you are likely to want to run the aspect in another AOP framework.
The main advantage of the more specific advice is that there is no need to invoke the
proceed()method and, therefore, no possibility of inadvertently failing to proceed down the interceptor chain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With