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 advices available in AOP. If a method exits by throwing an exception, the after throwing advice is executed.

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

Example of @AfterThrowing Advice

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

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

  • Create the bean

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

Account.java

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

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

StudentAccountDao.java

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class StudentAccountDao {
public List findAcc(boolean obj) {
if(obj) {
throw new RuntimeException(" New Exception ");
}
List<Account> myacc = new ArrayList<Account>();
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 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_AfterThrowing")
public class DemoAOP {
} 
  • Create an aspect class for aspect logic

In this step, we are going to create a class named AspectClass, which contains the aspect logic (all advice types).

AspectClass.java

import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectClass {
@AfterThrowing(pointcut = "execution(* com.app.SpringAOP_AfterThrowing.*.*(..))", 
throwing = "theException")
public void afterThrowingFindAccount( JoinPoint joinpoint, Throwable theException) {
String string = joinpoint.getSignature().toShortString();
System.out.println(" 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 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 myacc = null ;
 try {
 // add a boolean flag
 boolean obj = true;
 myacc =  studacc.findAcc(obj);
 }
catch(Exception theException) {
System.out.println("-----------");
System.out.println(" Caught the Exception " + theException );
}
appcontext.close();
}
} 

Output

Example of @AfterThrowing Advice