Wednesday, August 11, 2010

Spring AOP Interception

According to this post, most important - proxy-based AOP only works from "outside". Internal method calls are never intercepted.

This means that only the methods of your Spring beans can be intercepted. If this is not a problem, then here is an example.

Java
package com.my.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.ThrowsAdvice;

/**
* A Spring AOP advice to log service exception.
*/
@Aspect
public final class LoggingAdvice implements ThrowsAdvice {

/**
* Log.
*/
private Log log = LogFactory.getLog(this.getClass());

/**
* Log AxisFault detail after it is thrown.
*
* @param method calling method
* @param args method arguments
* @param target target
* @param ex AxisFault
* @throws Throwable throwable
*/
@AfterThrowing(pointcut="execution(* com.my.service.MyService.*(..))", throwing="ex")
public void afterThrowing(final Throwable ex) {
log.error(System.getProperty("line.separator") + "*****" + ex.getMessage());
}
}


Spring applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="loggingAdvice" class="my.com.aop.LoggingAdvice" />

No comments:

Post a Comment