Spring/spring_old

09./ Spring Web MVC-Aop

slow333 2023. 1. 15. 13:59

Aspect Oriented Programming

공통관심사항과 핵심관심 사항을 분리(cross-cutting concern, core concern)

 

모든 클래스에 공통 관심 사항을 적용하기 위해 사용

 

예: 모든 클래스의 동작 시간을 측정하는 것을 전부 적용하기....

aop 없으면 모든 코드에 동일/반복 코드를 넣어야 

@Aspect
@Component
public class TimeTraceAop {

  @Around("execution(* com.myvms..*(..))") // com.myvms 패키지의 모든 클래스에 적용
  public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
    long start = System.currentTimeMillis();
    System.out.println("Start : " + joinPoint.toString());
    try {
      Object ojb = joinPoint.proceed();
      return ojb;
    } finally {
      long finish = System.currentTimeMillis();
      long timeMs = finish - start;
      System.out.println("End: " + joinPoint.toString() + " " + timeMs + "ms");
    }
  }
}