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.

Account.java

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
@Override
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.

StudentAccountDao.java

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
@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" ); 
myacc.add(ac1);
myacc.add(ac2);   
myacc.add(ac3);
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.

DemoAOP.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@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).

AspectClass.java

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;
@Aspect
@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.

App.java

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 ");
System.out.println("-----------");
 appcontext.close();
}
} 

Output

Create a class that contains the Aspect logic

← PrevNext →


Related Topics

Spring MVC Example

Spring MVC Example We have previously discussed the Spring Web MVC framework. In this tutorial, we are going to create a simple example of Spring Web MVC. Following are the steps used to create an example...

2 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring MVC Form Checkbox

Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same...

12 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.

Spring – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.

Spring AOP – @After Advice

Spring AOP – @After Advice We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice...

3 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring – Annotation-based Inversion of Control (IoC)

Spring – Annotation-based Inversion of Control (IoC) We have previously discussed an example of Spring IoC (Inversion of Control). In the previous case, we have used the bean-configuration file to define the bean...

3 minutes read.

Spring Framework Annotations

Spring Framework Annotations Spring framework provides a variety of annotations for a better approach. Using annotations, we can develop Spring applications (small & large scale) easily with less code. Following are the annotations...

6 minutes read.

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring MVC Form Text Field

We have previously discussed the Spring MVC form tags. The form tag library provides a variety of form tags that are used to build web applications. One of them is a <form:input>...

8 minutes read.

Spring - Bean Scope

Spring - Bean Scope Bean Scope refers to a lifecycle of the bean. It allows us to have control over the creation of bean objects. It is a very powerful approach and provides...

6 minutes read.

Spring AOP – @Before Advice Type

Spring AOP – @Before Advice Type As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology,...

2 minutes read.

Spring MVC Number Range Validation

Example of Number Range Validation Here, we are going to create an example of number range validation. Following are the steps to create an example of number range validation. Create a request page In this step, we are...

12 minutes read.

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...

3 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring MVC Form Validation

Spring Validation is used to validate the @Controller inputs. It is used to check and accept the user input in any web application. Using Spring validation, we can validate the user input on...

12 minutes read.

Spring MVC – @RequestParam

We have already discussed an example of Spring MVC in our previous tutorials. Here, we are going to discuss the @RequestParam annotation. In the last case, we had created a bean class for...

8 minutes read.