공통 관심 사항과 핵심 로직을 분리하기 위한 관점 지향 프로그래밍 기법. AOP는 다음의 3가지 개념으로 이루어진다.
Advice - 실행될 고통 로직 (ex. 실행 시간 측정 등)
Pointcut - Advice가 적용될 대상 (ex. execution(* hello.hello_spring..*(..)))
Aspect - Advice 와 PointCut을 합친 개념 = AOP 클래스
/**
* 공통 관심 사항과 핵심 관심 사항을 분리하고 싶을 떄 AOP를 사용
* ex 모든 메서드의 호출 시간 측정
* 컨트롤러가 서비스에 접근할 때, 가짜 서비스인 프록시를 생성해서 접근하게 함
*/
@Aspect
@Component // 컴퍼넌트 스캔을 통해 스프링 빈으로 등록됨
public class TimeTraceApp {
// 하위 패키지에 대해 모두 적용 가능
@Around("execution(* hello.hello_spring..*(..))")
// 실제 실행될 메서드를 ProceedingJoingPoint를 통해 감쌈
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");
}
}
}