공통 관심 사항과 핵심 관심 사항을 분리
@Aspect
public class TimeTraceAop {
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("finish : " + joinPoint.toString() + " " + timeMs + "ms");
}
}
}
🔸 직접 Spring bean에 등록
@Bean
public TimeTraceAop timeTraceAop() {
return new TimeTraceAop();
}
🔸 @Component
어노테이션 사용
@Aspect @Component public class TimeTraceAop { ... }
@Around
어노테이션
@Around("execution(* hello.hellospring..*(..))")
: hello.hellospring 패키지 내의 모든 메서드에 적용