package com.spring.TestAOP1.aop;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopTest {
@Pointcut(value="execution(* com..AopTestServiceImpl.*(..))")
private void logPointcut() {}
private long startTime;
@Before("logPointcut()")
public void logBefore(JoinPoint joinPoint) {
startTime = System.currentTimeMillis();
System.out.println("================================================");
String methodName = joinPoint.getSignature().getName();
System.out.println("실행하는 핵심코드(메소드) : " + methodName);
System.out.println("(공통관심사)>>>Log : " + methodName + "("+ new Date() +")");
System.out.println("================================================");
}
@AfterReturning(pointcut = "logPointcut()", returning = "result")
public void logResult(JoinPoint joinPoint, Object result) {
long end = System.currentTimeMillis();
System.out.println("================================================");
String methodName = joinPoint.getSignature().getName();
System.out.println("(공통관심사)>>>Log : " + methodName + "("+ new Date() +")");
System.out.println("핵심코드가 반환한 값 : " + result.toString());
System.out.println("실행 시간 : " + (end - startTime) + "ms");
System.out.println("================================================");
}
@Around("logPointcut()")
public Object timeCheck(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("================================================");
System.out.println("Around Before 시간 : " + new Date());
Object result = pjp.proceed();
long end = System.currentTimeMillis();
System.out.println("Around After 시간 : " + new Date());
System.out.println("Around 실행 시간 : " + (end - start) + "ms");
System.out.println("================================================");
return result;
}
}
첫댓글 aop는 어떤 업무에 적용하면 편리할지 업무분석 설계를 잘해서 적용하시면 됩니다. 수고하셨어요