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 that executes regardless of method outcome (normal or exceptional return).

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

Example of @After Advice

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

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

  • Create a bean

In this step, we are going to create a bean named Account, that contains the bean object.


public class Account {
private String name;
private String code;
public Account() { }
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;
public String toString() {
return "Account [name=" + name + ", code=" + code + "]";
  • Create a DAO class

In this step, we are going to create a DAO class with the name StudentAccountDao, that contains the business logic of the application.


import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
public class StudentAccountDao {
public List findAcc(boolean obj) {
if(obj) {
throw new RuntimeException(" New Exception ");
List myacc = new ArrayList<Account>();
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 with the name DemoAOP, that performs the component scanning.


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
public class DemoAOP {
  • Create an aspect class for aspect logic

In this step, we are going to create a class with the name AspectClass, that contains the aspect logic (types of advices).


import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
public class AspectClass {
@After( " execution(* com.app.SpringAOP_After.*.*(..)) ")
public void afterFindAccount(JoinPoint thejp ) {
String  s = thejp.getSignature().toShortString();
System.out.println(" Executing the @After advice on the method :" + s);
@AfterThrowing(pointcut ="execution(* com.app.SpringAOP_After.*.*(..))", 
throwing = "theException") 
                    public void afterThrowingFindAccount(JoinPoint joinpoint, Throwable theException) {
String string = joinpoint.getSignature().toShortString();
System.out.println(" \n Executing the @AfterThrowing on method : " + string);
System.out.println(" The Exception is : " + theException); 
  • Create the main class

In this step, we are going to create the main class with the name 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 myacc = null ;
try {
// add a boolean flag
boolean obj = true;
myacc =  studacc.findAcc(obj);
catch(Exception theException) {
System.out.println(" Caught the Exception " + theException );


The following output shows that the @After advice is executed on the method containing exception.

After advice is executed

The following output shows that the @After advice is executed on the method without exception.

Create an aspect class for aspect logic