Spring AOP after returning advice example




After-returningadvice is an Advice that executes after the successful return of the method.


It is also possible to get hold of return value within the advice body. Use the returning attribute to specify the name of the parameter to which the exception should be passed:

<aop:config>
<aop:aspect ref="loggingAspect">
<aop:pointcut id="logPointCut" expression="execution(public * *(..))" />
<aop:after-returning returning="returnVal"  pointcut-ref="logPointCut" method="log" />
</aop:aspect>
</aop:config> 

The log
() method must declare a parameter 
returVal. For example, the method signature may be declared as:
public void log(String returnVal)


In Annotation driven approach, returning attribute should be given in @AfterReturning annotation
Example: @AfterReturning(pointcut ="execution(public * *(..))",returning="returnVal")
The advice method method must declare a parameter returnVal.
Example: public void afterReturningAdvice(String returnVal){....


There are two ways to create an Advice.
  • Annotation driven
  • XML configuration driven

Following Spring after-returning advice example explains both the approaches.


Below screenshot shows the project structure;




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" />
 <bean id="loggingAspect1" class="com.sample.spring.aop.LogginAspectAnnotationDriven" />
 <aop:aspectj-autoproxy />
 <aop:config>
  <aop:aspect ref="loggingAspect">
   <aop:pointcut id="logPointCut" expression="execution(public * *(..))" />
   <aop:after-returning returning="returnVal"  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;
 }
}

Advice class for XML driven approach:
/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.JoinPoint;
/**
 * @author Prabu
 * 
 */
public class LoggingAspect {
 public void log(JoinPoint jp,String returnVal) throws Throwable {
  System.out.println("Return value:"+returnVal);
  System.out.println("XML Configuration driven: After returning "+jp.getSignature().getName()+"()");
 }
}

Advice class for annotation driven approach:
/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

/**
 * @author Prabu
 *
 */
@Aspect
public class LogginAspectAnnotationDriven {
 
 @AfterReturning(pointcut ="execution(public * *(..))",returning="returnVal")
 public void afterReturningAdvice(JoinPoint jp,String returnVal){
  System.out.println("Return value:"+returnVal);
  System.out.println("Annotation driven:After returning "+jp.getSignature().getName()+"()");
 }
}

Output:


Source code of the application:


SpringAOPAfterReturningAdvice.zip






Reactions:

0 comments :

Post a Comment