각 메소드의 실행 시간을 측정하려면?
StopWatch
@Override
public void register(Member member) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
memberStorage.store(member);
stopWatch.stop();
System.out.println("stopWatch.getTotalTimeMillis() = " + stopWatch.getTotalTimeMillis());
}
메소드마다 작성하기에는 너무나 번거롭다
⇒ AOP (Aspect Oriented Programming)
설정
dependencies {
// ...
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
}
이 버튼 눌러서 gradle를 적용하자.
@Aspect
@Component
public class ElapsedTimeMeasureAop {
// @Around("execution(* com.kit.dorm..*(..))")
@Around("@annotation(com.kit.dorm.annotation.ElapsedTimeLog)")
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try{
Object result = joinPoint.proceed();
return result;
} finally {
stopWatch.stop();
System.out.println(joinPoint.toString() + " stopWatch.getTotalTimeMillis() = " + stopWatch.getTotalTimeMillis());
}
}
}
joinPoint → 비즈니스 로직
package com.kit.dorm.annotation;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ElapsedTimeLog {}
@Around("@annotation(com.kit.dorm.annotation.ElapsedTimeLog)")
ElapsedTimeLog가 붙여 있는 메소드만 적용
Bean을 Proxy 기반으로 만들어 낸다.
Advice에 따라 AOP가 실행되는 위치가 달라진다.
Weaving을 통해 타겟을 가져온다