by BehindJava

What are the types of Advices in AOP Spring Boot Application

Home » springboot » What are the types of Advices in AOP Spring Boot Application

In this tutorial we are going to learn about implementing the AOP i.e., Aspect Oriented Programming in the Spring Boot application.

AOP implementation in Spring Boot application

The best approach in implementing the AOP is Spring Boot application is using the AspectJ Annotations. This is a complete annotation based approach. Moreover AspectJ is an aspect-oriented programming (AOP) library as extension created at PARC for the Java programming language.

It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become very popular and widely used effective standard for AOP by highlighting simplicity and usability among developers. It uses Java-like syntax, and included IDE integrations for displaying cross-cutting structures.

Types of advices

We have five types of Advice as given below. Types of Advice are on the basis of all possibilities in execution of a business method.
Before Advice
Before Advice executes before the execution of business method. Once Advice execution completes method execution will take place automatically.
After Advice
After Advice executes after the execution of business method. Once method execution completes advice execution will take place automatically.
Around Advice
Around Advice executes in two parts. Some part executes before the execution of business method whereas other part executes after the execution of business method.
After Returning Advice
It executes after the execution of business method but only if method executes successfully.
After Throwing Advice
It executes after the execution of business method but only if method fails to execute successfully.
♦ Although we can have any number of either same type of advice or of different type connected to a single business method.

Differences among the After, After Returning & After Throwing Advices

After Advice mandatorily executes after the execution of business method but other two advices may or may not execute after the execution of business method. After Returning Advice will execute only on successful execution of business method whereas After Throwing Advice will execute only on failure in execution of business method.