Step One: Create a System architecture class which defines which methods to pointcut
@Aspect
public class SystemArchitecture {
/**
* Service Layer Aspect.
*/
@Pointcut("within(com.service..*)")
public void inServiceLayer() {
}
/**
* Service Implementation layer aspect.
*/
@Pointcut("execution(* com.service..*.*(..))")
public void businessService() {
}
/**
* DAO Layer aspect.
*/
@Pointcut("execution(* com.dao..*.*(..))")
public void dataAccessOperation() {
}
}
Step two: Create a Tracing Aspect class which contains the actual code that is executed at the point cuts
@Aspect
public class TracingAspects {
/**
* Around advice to log method entry and exits. Logs at TRACE level
*
* @param pjp ProceedingJoinPoint
* @return Object
* @throws Throwable if aspect throw any exception
*/
@Around("com.util.SystemArchitecture.dataAccessOperation()")
public Object traceMethodEntryExit(ProceedingJoinPoint pjp) throws Throwable {
Log log = LogFactory.getLog(pjp.getTarget().getClass());
MethodSignature mthdSignature = (MethodSignature) pjp.getSignature();
log.debug("Entering method " + mthdSignature.getName());
try {
return pjp.proceed();
} finally {
log.debug("Exiting method " + mthdSignature.getName());
}
}
/**
* round advice to log method execution time. Logs at DEBUG level
*
* @param pjp ProceedingJoinPoint
* @return Object
* @throws Throwable if aspect throw any exception
*/
@Around("com.util.SystemArchitecture.dataAccessOperation()")
public Object perfTrace(ProceedingJoinPoint pjp) throws Throwable {
Log log = LogFactory.getLog(pjp.getTarget().getClass());
MethodSignature mthdSignature = (MethodSignature) pjp.getSignature();
long startTime = System.currentTimeMillis();
try {
return pjp.proceed();
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
log.debug("Method " + mthdSignature.getName() + " took " + elapsedTime + "ms");
}
}
Step Three: Enable AOP in Spring configuration
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="tracingAspects" class="com.util.TracingAspects"/>
</beans>
No comments:
Post a Comment