![]() |
VOOZH | about |
After Returning Advice runs only after a method executes successfully. It is implemented using the @AfterReturning annotation.
If the method throws an exception, this advice will not execute.
Open Spring Initializr and provide the following details:
Download the project and extract the ZIP file and import the Project into IDE
👁 ImageAdd Spring AOP dependency in pom.xml.
pom.xml
Create package: com.after_returning_advice.model and add Student class to represent student data.
Student class:
Create package: com.after_returning_advice.service and Add StudentService class with a method to add students using name arguments
StudentService class:
Create package: com.after_returning_advice.controller and add StudentController to handle GET requests and call the service method
StudentController class:
Create package: com.after_returning_advice.aspect and add StudentServiceAspect class.
Define:
StudentServiceAspect class:
Run the project as a Spring Boot Application.
👁 ImageFor the demo, we are hitting URL with fname as Harry and sname as Potter. In this case, the method will be executed normally.
👁 ImageWhen we hit URL with fname as Tom, the service method will throw an exception. The After Returning advice will not be executed.
👁 ImageAs seen in the output, after returning advice is called after the service method is executed successfully.