Around Advice Example Spring AOP


The same bean program of AOP Advices – Types – Tutorial is used. Change comes in configuration XML file and also slightly in client program. Let us name the modified XML file as applicationContext4.xml.

This is equivalent to all the three advices, Before advice, After returning advice and After Throwing Advice and executes before and after each bean method. Now add a new class AroundAdvice implementing MethodInterceptor interface and override the abstract method invoke(). You must call proceed() method to execute the original bean method; else the bean method is not called. Like this, the around advice can control the bean program execution. For this reason, around advice is used very often among all advices. Around Advice is represented by the interface MethodInterceptor. In this program, let us call all the advices (not only around advice).

1. The bean program used in AOP Advices – Types – Tutorial is used and reproduced here.

Bean Program – File Name: Student.java

public class Student
{
  String name;         				// just two variables
  String college;

  public void setName(String name)    		// just two set methods
  {
    this.name = name;
  }
  public void setCollege(String college)
  {
    this.college = college;
  }
  public void printName()            		// 3 print methods to print variables
  {
    System.out.println("Student Name is " + name);
  }
  public void printCollege()
  {
    System.out.println("Student College is " + college);
  }
  public void printThrowException()
  {
    throw new IllegalArgumentException();
  }
}

Add a new class – AroundAdvice.java

import java.util.Arrays;    
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor 
{
  public Object invoke(MethodInvocation methodInvocation) throws Throwable 
  {
    System.out.println("Method name is " + methodInvocation.getMethod().getName());
    System.out.println("Method arguments: " + Arrays.toString(methodInvocation.getArguments()));
 
    System.out.println("Around advice in action");
 
    try     		// now call the original bean method
    {		 
      Object result = methodInvocation.proceed();   // very important to call
      System.out.println("Around advice in action\n");
      return result;
    } 
    catch (IllegalArgumentException e) 
    {
      System.out.println("catch block executed. " + e);
      throw e;
    }  
  }
}

invoke() is a predefined abstract callback method of interface MethodInterceptor

Configuration file: applicationContext4.xml





  
  






  
  
    
      aa
    
  


Client Program: Client5.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client5
{
  public static void main(String[] args)
  {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext4.xml");

    Student std1 = (Student) ac.getBean("aaproxy");

    std1.printName();
    std1.printCollege();
    try
    {
      std1.printThrowException();
    }
    catch (Exception e) {  }
  }
}

Around Advice

The screenshot is very clutter as all the three print methods are called of bean program. Let us separate for one method, say printName(), and analyse the output.

See the bean code:

  System.out.println("Method name is " + methodInvocation.getMethod().getName());
  System.out.println("Method arguments: " +     Arrays.toString(methodInvocation.getArguments()));
 
  System.out.println("Around advice in action");
 
  try 
  {
    Object result = methodInvocation.proceed();
    System.out.println("Around advice in action\n");

 

The getMethod().getName() of MethodInvocation parameter returns the name of the method which is invoked. It is here printName().

getArguments() method of MethodInvocation returns parameter list of printName() method. As there are no parameters, it printed empty, [].

The proceed() method calls the actual method printName() and printName() is executed and the output is printed.

"Around advice in action" is called before and also after calling printName() method (two times).

With this AOP theory and practice, I am sure you are well aware of the AOP terminology like joinpoint, concern, aspect, advice etc. Let us go more with pointcut, method matcher, StaticMethodMatcherPointcut etc. later.

Leave a Comment

Your email address will not be published.