Spring AOP after throwing advice example


After-throwing advice is an Advice that executes id a method exists by throwing an exception. 


It is also possible to get hold othrown exception within the advice body. Use the throwing attribute to specify the name of the parameter to which the exception should be passed:
<aop:aspectid="afterThrowingExample" ref="aBean">
<aop:after-throwing pointcut-ref="my_pointcut" throwing="my_exception" method="doMyActions"/> ...
</aop:aspect> 


The 
doMyActions() method must declare a parameter my_exception. For example, the method signature may be declared as:
public void doMyActions(DataAccessException 
my_exception) {... 


In Annotation driven approach, throwing attribute should be given in @AfterThrowing annotation
Example: @AfterThrowing(pointcut = "execution(public * *(..))",throwing = "nullPointEx")
The advice method method must declare a parameter nullPointEx.
Example: public void afterThrowingAdvice(JoinPoint jp,NullPointerException nullPointEx){...


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

Following Spring after-throwing 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-throwing throwing="nullPointEx"  pointcut-ref="logPointCut" method="log" />
  </aop:aspect>
 </aop:config>
</beans>

Business interface:
/**
 * 
 */
package com.sample.spring.business;

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

Business class implementation:
/**
 * 
 */
package com.sample.spring.business;

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

 @Override
 public int sayHello(String name) {
  String tst=null;
  return tst.length();
 }
}

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,NullPointerException nullPointEx) throws Throwable {
  System.out.println("\nException is:"+nullPointEx.getLocalizedMessage());
  System.out.println("XML Configuration driven: After throwing "+jp.getSignature().getName()+"()");
 }
}


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

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

/**
 * @author Prabu
 *
 */
@Aspect
public class LogginAspectAnnotationDriven {
 
 @AfterThrowing(pointcut = "execution(public * *(..))",throwing = "nullPointEx")
 public void afterThrowingAdvice(JoinPoint jp,NullPointerException nullPointEx){
  System.out.println("Exception is "+nullPointEx.getLocalizedMessage());
  System.out.println("Annotation driven:After throwing "+jp.getSignature().getName()+"()");
 }
}

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"); 
  int result=tester.sayHello("Prabu");
  System.out.println(result);
 }

}


Output:


Source code of this application:

SpringAOPAfterThrowingAdvice.zip






Reactions:

1 comment :