Spring Boot AOP After Returning Advice
After returning is an advice in Spring AOP that invokes after the execution of join point complete (execute) normally. It does not invoke if an exception is thrown. We can implement after returning advice in an application by using @AfterReturning annotation. The annotation marks a function as an advice to be executed before the method covered by PointCut.
After returning advice runs when a matched method execution returns a value normally. The name that we define in the return attribute must correspond to the name of a parameter in the advice method. When a method returns a value, the value will be passed to the advice method as the corresponding argument value.
Let's implement the after returning advice in an application.
Spring Boot After Returning Advice Example
Step 1: Open Spring Initializr http://start.spring.io.
Step 2: Provide the Group name. We have provided the Group name com.javatpoint.
Step 3: Provide the Artifact Id. We have provided the Artifact Id aop-after-returning-advice-example.
Step 4: Add the Spring Web dependency.
Step 5: Click on the Generate button. When we click on the Generate button, it wraps all the specifications in a jar file and downloads it to the local system.
Step 6: Extract the downloaded jar file.
Step 7: Import the folder by using the following steps:
File -> Import -> Existing Maven Projects -> Next -> Browse the Folder aop-after-returning-advice-example -> Finish.
Step 8: Open the pom.xml file and add the following AOP dependency. It is a starter for aspect-oriented programming with Spring AOP and AspectJ.
Step 9: Create a package with the name com.javatpoint.model in src/main/java folder.
Step 10: Create a class with the name Account in the package com.javatpoint.model.
In the Account class, do the following:
Step 11: Create another package with the name com.javatpoint.service.impl.
Step 12: In this package, create a class with the name AccountServiceImple.
In this class, we have defined account service.
Step 13: Create an interface with the name AccountService in the package com.javatpoint.service.impl.
Step 14: Create a package with the name com.javatpoint.aspect.
Step 15: Create a class with the name AccountAspect in the package com.javatpoint.aspect.
In this class, we have implemented the after returning advice by using the annotation @AfterReturning. We have also defined a method afterReturningAdvice() method.
Note: The name (account) that we define in the returning attribute must correspond to the name of a parameter in the advice method.
Step 16: Open the AopAfterReturningAdviceExampleApplication.java file and add an annotation @EnableAspectJAutoProxy.
The annotation enables support for handling components marked with AspectJ's @Aspect annotation. It is used with @Configuration annotation.
We have used the proxyTargetClass attribute of the annotation @EnableAspectJAutoProxy. The attribute proxyTargetClass=true allows us to use CGLIB (Code Generation Library) proxies instead of the default interface-based JDK proxy approach.
ConfigurableApplicationContext is an interface that provides facilities to configure an application context in addition to the application context client methods in the ApplicationContext.
After creating all the class and packages, the project directory looks like the following:
Step 17: Open the AopAfterReturningAdviceExampleApplication.java file and run it as Java Application. It shows the output, as shown below:
In the next section, we will understand after throwing advice.