Mar 13 2008
Spring AOP with aspectJ and JDK5 annotations
The logger aspect
loggeraspect.java
The intention of this aspect is to explain the concepts of pointcut and the different code advices:
JAVA:
-
package be.loadit.aspects;
-
-
import org.apache.log4j.Logger;
-
import org.aspectj.lang.ProceedingJoinPoint;
-
import org.aspectj.lang.annotation.AfterReturning;
-
import org.aspectj.lang.annotation.AfterThrowing;
-
import org.aspectj.lang.annotation.Around;
-
import org.aspectj.lang.annotation.Aspect;
-
import org.aspectj.lang.annotation.Before;
-
import org.aspectj.lang.annotation.Pointcut;
-
-
@Aspect
-
public class LoggerAspect {
-
private final Logger logger = Logger.getLogger(getClass());
-
-
//Pointcut definitions on a method signature basis
-
//All public methods
-
@Pointcut("execution(public * *(..))")
-
private void publicMethod() {}
-
-
//All public setters
-
@Pointcut("execution(public * set*(..))")
-
private void publicSetMethod() {}
-
-
//All public getters
-
@Pointcut("execution(public * get*(..))")
-
private void publicGetMethod() {}
-
-
@Pointcut("within(be.loadit.service..*)")
-
public void inServiceLayer() {}
-
-
//The code advices
-
-
@Before("inServiceLayer()")
-
public void beforePublic(){
-
if(logger.isDebugEnabled()) {
-
logger.debug("Before method call");
-
}
-
}
-
-
@AfterReturning("publicMethod()")
-
public void afterPublicReturning(){
-
if(logger.isDebugEnabled()) {
-
logger.debug("After returning");
-
}
-
}
-
-
@AfterThrowing(pointcut="publicMethod()",throwing="exception")
-
if(logger.isDebugEnabled()) {
-
logger.debug("After throwing");
-
}
-
}
-
-
//Before and after = around
-
//&& !publicSetMethod() && !publicGetMethod()"
-
@Around("publicMethod()")
-
-
if(logger.isDebugEnabled()) {
-
logger.debug("Around: before");
-
logger.debug("Calling " + pjp.getSignature().getName());
-
for (int i = 0; i <args.length; i++) {
-
logger.debug("with arg: " +args[i].toString());
-
}
-
-
}
-
-
-
if(logger.isDebugEnabled()) {
-
logger.debug("Around: after");
-
logger.debug("Calling " + pjp.getSignature().getName());
-
}a
-
return retVal;
-
}
-
-
}
Our test class
persontest.java
JAVA:
-
package be.loadit.tests;
-
import java.util.Observer;
-
import org.junit.Test;
-
import org.springframework.test.context.ContextConfiguration;
-
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
-
import be.loadit.service.Person;
-
public class PersonTest extends AbstractJUnit4SpringContextTests {
-
@Test
-
public void testPerson() {
-
Person person =(Person)applicationContext.getBean("Person");
-
person.setName("Gilles");
-
person.setAge(29);
-
}
-
}
Output
CODE:
-
0 [main] DEBUG be.loadit.aspects.LoggerAspect - Before method call
-
0 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: before
-
0 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling setName
-
0 [main] DEBUG be.loadit.aspects.LoggerAspect - with arg: Gilles
-
Monitoring Call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - After returning
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: after
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling setName
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Before method call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: before
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling setAge
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - with arg: 29
-
Monitoring Call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - After returning
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: after
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling setAge
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Before method call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: before
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling getName
-
Monitoring Call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - After returning
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: after
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling getName
-
Name: Gilles
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Before method call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: before
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling getAge
-
Monitoring Call
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - After returning
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Around: after
-
16 [main] DEBUG be.loadit.aspects.LoggerAspect - Calling getAge
-
Age: 29

