✅ @Aspect ✅ @Around
예를 들어 모든 메소드의 호출 시간을 측정해야 하는 상황이라고 하자
그러면 모든 메소드에 시간 측정 로직을 추가해야 할까..?
그래도 되지만 당연히 효율적이지 않다. 이때 필요한게 AOP!
Aspect Oriented Programming
공통 관심 사항과 핵심 관심 사항을 분리하여 원하는 곳에 공통 관심 사항을 적용한다.
@Component
@Aspect
public class TimeTraceAop {
@Around("execution(* hello.hellospring..*(..))")
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("START: " + joinPoint.toString());
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
long timeMs = finish - start;
System.out.println("END: " + joinPoint.toString()+ " " + timeMs + "ms");
}
}
}