Spring AOP Around Advice Example



Around advice is the most powerful advice in Spring AOP. The reason is, this advice can perform custom behavior before and after the method invocation
This is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception

One more important feature is, it is possible to get hold of the method parameters. And the method parameters can be modified or new parameters can be passed.

Do you have any one of the below question in your mind?

1. How to inject your own code before and after a method execution?
2. How to modify a method parameters before the method execution starts?
3. How yo modify (or) send your own return value?
4. How to validate method parameters before the method executions?
The answer for all the above questions is"Around advice". The main thing here is the calling method or the target method will not aware of any of these activities.

So this is the most powerful advice in spring AOP.

In the below example program, the around advice gets all the method parameters and modifies it before the control goes to actual business method. Also it modifies the return vale from the business method and sends the modified value to the calling point.

Project structure is given below;


XML Configuration driven approach:

Configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 <bean id="businessClass" class="com.sample.spring.business.BusinessClass">
 </bean>

 <bean id="loggingAspect" class="com.sample.spring.aop.LoggingAspect" />
 <aop:aspectj-autoproxy />
 <aop:config>
  <aop:aspect ref="loggingAspect">
   <aop:pointcut id="logPointCut" expression="execution(public * *(..))" />
   <aop:around pointcut-ref="logPointCut" method="log" />
  </aop:aspect>
 </aop:config>
</beans>


Business interface:

 /*** 
 */
package com.sample.spring.business;

/**
 * @author Prabu
 *
 */
public interface BusinessInterface {
 public String sayHello(String name);
}


Business class implementation:

 /*** 
 */
package com.sample.spring.business;

/**
 * @author Prabu
 *
 */
public class BusinessClass implements BusinessInterface {

 @Override
 public String sayHello(String name) {
  return "Hello "+name;
 }
}

Aspect class:

To get the method parameters ProceedingJoinPoint has a method 'getArgs()'. This will give you the method parameters as an array of Object. You can modify these values if you want. jp.proceed() is used to continue the normal method execution. You can pass the modified arguments to this method,it will be reflected in the business class. The return value of this jp.proceed() will be given back to the calling point. You can modify the return value also.
I this example I am passing one parameter to the business method. I am modifying it in my around advice and sending the modified value to the business class. I am getting the return value in my advice and modifying the return value before sending it back to the calling point.

/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
/**
 * @author Prabu
 * 
 */
public class LoggingAspect {
 public String log(ProceedingJoinPoint jp) throws Throwable {
  System.out.println("Spring AOP: Around advice");
  Object[] args=jp.getArgs();
  if(args.length>0){
  System.out.print("Arguments passed: ");
   for (int i = 0; i < args.length; i++) {
    System.out.print("Arg"+(i+1)+":"+args[i]);
    args[i]=":Spring AOP removed the argument";
   }
  }
  Object result=jp.proceed(args);
  return result.toString()+" :Result is also modified";
 }
}


Driver for this application:

/**
 * 
 */
package com.sample.spring.driver;

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

import com.sample.spring.business.BusinessInterface;

/**
 * @author Prabu
 *
 */
public class AOPTester {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext contex = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 
  BusinessInterface tester=(BusinessInterface) contex.getBean("businessClass"); 
  String result=tester.sayHello("Prabu");
  System.out.println("\n"+result);
 }

}

Output:



Annotation driven approach:

Configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 <bean id="businessClass" class="com.sample.spring.business.BusinessClass">
 </bean>

 <bean id="loggingAspect" class="com.sample.spring.aop.LogginAspectAnnotationDriven" />
 <aop:aspectj-autoproxy />
</beans>

Aspect class:

/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

/**
 * @author Prabu
 *
 */
@Aspect
public class LogginAspectAnnotationDriven {
 
 @Around("execution(public * *(..))")
 public String beforeAdvice(ProceedingJoinPoint jp) throws Throwable{
  System.out.println("Annotation driven:Around advice");
  Object[] args=jp.getArgs();
  if(args.length>0){
  System.out.print("Arguments passed:");
   for (int i = 0; i < args.length; i++) {
    System.out.print("Arg"+(i+1)+":"+args[i]);
    args[i]=":Annotation driven argument";
   }
  }
  Object result=jp.proceed(args);
  return result.toString()+" :Result is also modified";
 }
}

Output:


Source code of this application:
SpringAOPAroundAdviceXML.zip
SpringAOPAroundAdviceAnnotation.zip

           Do you like to add your own tutorials here? Here is an opportunity... Check this link ! !             






Reactions:

0 comments :

Post a Comment

Ads