package com.cv.spring.aop.annotation;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
/**
* @author Chandra Vardhan
*/
@Aspect
public class LoggingAspect {
private final static Logger LOGGER = Logger.getLogger(LoggingAspect.class);
@Pointcut("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
public void performance() {
}
@Before("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
public void logBefore() {
LOGGER.info("*********************************************");
System.out
.println("logBefore() is called before actual method execute...");
LOGGER.info("**********************************************");
}
@After("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
public void logAfter() {
LOGGER.info("*********************************************");
System.out
.println("logAfter() is called After actual method execute...");
LOGGER.info("**********************************************");
}
@Before("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..)) && !bean(logAspect)")
public void logBefore(JoinPoint joinPoint) {
LOGGER.info("*********************************************");
System.out
.println("logBefore() is called before actual method execute...");
LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
LOGGER.info("**********************************************");
}
@After("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
public void logAfter(JoinPoint joinPoint) {
LOGGER.info("logAfter() is running!");
LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
LOGGER.info("******");
}
@AfterReturning(pointcut = "execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerReturnValue(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
LOGGER.info("logAfterReturning() is running!");
LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
LOGGER.info("Method returned value is : " + result);
LOGGER.info("******");
}
@Around("execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerAround(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("logAround() is running!");
LOGGER.info("hijacked method : "
+ joinPoint.getSignature().getName());
LOGGER.info("hijacked arguments : "
+ Arrays.toString(joinPoint.getArgs()));
LOGGER.info("Around before is running!");
joinPoint.proceed();
LOGGER.info("Around after is running!");
LOGGER.info("******");
}
@AfterThrowing(pointcut = "execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerThrowException(..))", throwing = "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
LOGGER.info("logAfterThrowing() is running!");
LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
LOGGER.info("Exception : " + error);
LOGGER.info("******");
}
} |
No comments:
Post a Comment