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 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 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 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 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 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 - 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 - 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 - 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 - Injecting values from Property Files

Spring - Injecting values from Property Files In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource...

3 minutes read.

Spring – Configuration using @Configuration

Spring – Configuration using @Configuration We have previously discussed the Spring configuration using an XML configuration file. In that case, we have defined all the bean information in the bean-configuration file (applicationContext.xml). ...

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

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 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 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 – 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 MVC Regular Expression Validation

Example of Regular Expression Validation Here, we are going to create an example of regular expression validation. Following are the steps to create an example of a regular expression: Create a request page In this step, we are going...

13 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 – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 minutes read.

Spring Autowiring – Constructor Injection

Spring Autowiring – Constructor Injection We have previously discussed an example of Constructor Dependency Injection (CDI) using an XML-based configuration. In the previous case, we have defined the reference of the constructor in...

2 minutes read.