CS/김영한 스프링 강의
스프링 입문 - 섹션7 AOP
용나리
2023. 6. 29. 20:28
@Component
@Aspect
public class TimeTraceAop {
@Around("execution(* hello.hellospring..*(..))")
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");
}
}
}
핵심 관심사항과 공통 관심사항을 분리한다. @Around를 통해 어디 패키지의 어느 부분까지 공통으로 적용할 건지 정할 수 있음. @Aspect를 붙이면 되고, 파일 만들어 @Component로 컨테이너에 등록할 지, 이미 있는 config 클래스에 @Bean으로 등록할지는 취향. 데코레이터 비슷한거인듯.
저걸 하면 저 과정을 거치고 들어감. 프록시로 모방해서 저놈이 실행하는 거다.