@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
위와 같이 @LogExecutionTime을 달아준다
@Target(ElementType.METHOD) //타켓은 메소드
@Retention(RetentionPolicy.RUNTIME) //런타임중에 실행
public @interface LogExecutionTime {
}
@Component
@Aspect
public class LogAspect {
Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Around("@annotation(LogExecutionTime)")
//joinPoint == 해당 애노테이션이 붙은 메소드를 가르킨다 (@Around가 갖고있는 애노테이션)
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object proceed = joinPoint.proceed();
stopWatch.stop();
logger.info(stopWatch.prettyPrint());
return proceed;
}
}
실행시 해당메소드가 실행될 때 마다 설정한 프록시가 같이 구현된다, 즉 구현시간이 측정되어서 같이 출력되는 형태
2020-10-13 15:26:38.582 INFO 19353 --- [nio-8080-exec-8] o.s.samples.petclinic.owner.LogAspect : StopWatch '': running time = 70674 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000070674 100%