Spring/스프링 입문
[김영한 스프링] 19. AOP
개발게발
2023. 4. 1. 23:42

시간 측정 로직을 추가할 곳이 많으면 많을수록 유지보수 불가능

package hello.hellospring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
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");
}
}
}

hello.hellospring/aop/TimeTraceAop 생성
@Aspect
- AOP 사용하도록 함
@Component
- config파일에 Bean을 등록하지 않아도 사용 가능하도록 함
@Around()
- 괄호안에 해당하는 메소드의 실행 전, 후에 동작
joinPoint.proceed()
- 비지니스 메소드 호출

App 실행 시 시간 체크


회원 등록과 조회 시 시간 체크

기존에는 Controller에서 Service를 호출

프록시 Service라는 가짜 Service를 생성시켜서 Controller가 프록시 Service를 호출 후 실제 Service를 호출


[무료] 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 - 인프런 | 강의
스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., - 강의 소개 | 인프런
www.inflearn.com