Spring AOP – @AfterReturning Advice

Spring AOP – @AfterReturning Advice

We have discussed earlier the types of advices available in Spring AOP. One of them is After Returning advice. It is an advice that executes after the normal execution of a method (without any exception).

Let’s understand the concept of after returning advice with the help of an example.

Example of @AfterReturning Advice

Here, we are going to create an example of after returning advice using @AfterReturning annotation.

Following are the steps to create an example of @AfterReturning:

  • Create a bean

In this step, we are going to create a bean named Account, that stores an object. We have also defined two constructors: no-argument and parametrized constructor to the Account class.


public class Account {
private String name;
private String code;
//no-argument constructor
public Account() { }
//argument constructor 
public Account(String name, String code) {
this.name = name;
this.code = code;
public String getName() { 
return name;
public void setName(String name) {
 this.name = name;
public String getCode() {
return code; 
public void setCode(String code) {
this.code = code;
//defining toString() method
public String toString() { 
return "Account [name=" + name + ", code=" + code + "]";
  • Create the DAO class

In this step, we are going to create a DAO class named StudentAccountDao, that contains the business logic.


import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
public class StudentAccountDao {
public List<Account> findAcc() {
List myacc = new ArrayList();
Account ac1 = new Account( "jyotika", "ABC01" );
Account ac2 = new Account( "neha", "ABC05" );
Account ac3 = new Account( "shuhbham", "ABC06" ); 
return myacc ;
  • Create a class that performs component scanning

In this step, we are going to create a class named DemoAOP, that performs the component scanning.


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@ComponentScan( "com.app.SpringAOP_AfterReturn" )
public class DemoAOP {
  • Create a class that contains the Aspect logic

In this step, we are going to create a class named AspectClass, that contains the Aspect logic (advice types and pointcut expressions).


import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
public class AspectClass { 
@AfterReturning(pointcut = " execution(* com.app.SpringAOP_AfterReturn.*.*()) ", 
returning = "result" )
public void afterReturningFindAccount(JoinPoint jp, List result) {
String method = jp.getSignature().toShortString();
System.out.println("Executing the @AfterReturning on method : " + method );
System.out.println("\n Result : " + result);
  • Create the main class

In this step, we are going to create a main class named App, that contains the main method.


import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App 
public static void main( String[] args )
AnnotationConfigApplicationContext appcontext = new AnnotationConfigApplicationContext(DemoAOP.class);
// defining the Student dao class
StudentAccountDao studacc = appcontext.getBean("studentAccountDao", StudentAccountDao.class);
List<Account> myacc = studacc.findAcc(); 
System.out.println(" Main program - @AfterReturning ");


Create a class that contains the Aspect logic

← PrevNext →